Android - how to kill all actions when HOME is pressed?

I have an application that contains several actions.

At the moment, the entire application contains about 8 types of activities. I first show a splash screen for a few seconds where all the preferences are loaded and configured (from sharedPreferences) - they are stored in the Preferences class I made for this: this class contains just static variables, so all actions in the application can read them and change them as needed.

EDIT . Additional data is stored in this class as the application starts, some of it is executed from the web service - the data is parsed into obejcts, and references to these obejcts are stored in the settings class (or list of objects).

My problem is that when users press the HOME key, the current activity is put in the background. If an activity sits in the background for a long time (many users "close" applications by pressing "home" instead of "back") and then reopen it, it shows the activity that was started before HOME was pressed.

As an example, let's say a user launches an application, sees a Splash screen for a few seconds. The splash screen then starts a new activity and causes completion by itself.

This means that now the Activity stack is just MainActivity (main menu). In MainActivity, I provide all buttons with listeners in the onCreate method, and most of the buttons require some information from the above settings class.

When I then press HOME and reopen the app after a few hours, none of the buttons work anymore - the GUI seems to be responsive, etc., but something still goes wrong. One of the buttons, which should work even with all the settings being erased, will simply open a dialog with some text in it.

Listener:

Button b = (Button)v.findViewById(R.id.id_b1);
b.setOnClickListener(new OnClickListener() {
  public void onClick(View v) {
    Dialog dialog = createDialog(MainActivity.this, DIALOG_CONST1);
    dialog.show();
  }
});

      

I have a constant for dialog types as I am using some custom dialog projects that I created for a class to create dialogs for me.

From an action containing a button (simplified bit):

public static Dialog createDialog(final Context c, int dialogId) {
  Dialog dialog = null;
  CustomDialog.Builder customBuilder;
  switch (dialogId) {
    ...
    case d1:
      customBuilder = new CustomDialog.Builder(c, DIALOG_CONST1);

      //Sets up the parapters to create the dialog afterwards
      customBuilder.setTitle("Header").setMessage("Content")
      .setPositiveButton("", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
          dialog.dismiss();
        }
      });
      dialog = customBuilder.create(); //Creates the dialog from the above settings
      dialog.setCanceledOnTouchOutside(true);
      break;
    ...
  }
  ... //Set the width of the dialog
  return dialog;
}

      

The CustomDialog class extends the Dialog class and then, depending on the s supplied with it, inflates one of several dialog layouts and adds content, etc. Works fine, but after the app pauses HOME for a while and everything goes wrong, no dialog is shown when I press the button. I use the flash of the download dialog on other buttons, but then nothing happens - the dialog is not displayed. The weird thing is that if I press the button in the middle of the screen again, I cannot press it (the graphs are not responsive), but when I press once towards the side of the screen and then in the middle of the graph it reacts, the dialogs are canceled when pressed outside, so I wonder , is there some very subtle "transparent" dialogue in the middle or something else - I just don't know why this is going to happen.

EDIT . By actually looking at the variables that I store in the settings class, I see that I am storing the width and height in screen pixels. Width used to set the width of custom dialogs to a specific% of the screen width. If the settings get erased, that might explain why I can't see any dialogs since the width is 0 ... I really can't imagine what happens if it's not all my variables in the settings class that get erased - everything actually indicates on this.

dialog.getWindow().setLayout(Settings._widthpx - (Settings._widthpx/5), dialog.getWindow().getAttributes().height); //Width = 80%

      

In fact, I have to admit that I really don't know what is causing this as I am fairly new to Android. I suspect that the GC will remove all my variables after a while when the settings class has not been used, causing all settings to be removed when the user comes back a few hours later. However, it doesn't explain why the buttons don't work (one of them doesn't need to be configured).

This primary activity can start other activities, and they can start new activities again.

I think all my problems can be solved if I can just get the whole application to close when I press HOME and thus make it start from scratch whenever an icon is clicked - force listening for buttons and settings to load.

I read here about "android: clearTaskOnLaunch", "android: launchMode" and "android: finishOnTaskLaunch", but I'm not really sure how to use them correctly.

Anyone who could explain to me why the buttons are not working, or what could happen to the variables in my settings class when the app is in the background for a while, or maybe give me some advice on how to get it right "activity" settings.

EDIT : The app will be running Android 1.6+, so I can't use any new features or anything ...

Many thanks

+3


source to share


5 answers


  • DO NOT override master key functionality. Maybe you will find something somewhere that will allow you to do this. A God-fearing application that embraces standards does not override the home key in any way.
  • Just put android: clearTaskOnLaunch = true in your manifest. This will ensure that your main activity is triggered every time you click the launcher icon.

I suspect the GC will remove all my variables after a while when the settings class has not been used, causing all settings to be cleared out when the user comes back after a few hours



  • Android isn't wicked enough to do this to your app. It can kill your application, services and whatever is running in the background after some time of inactivity and / or the need for more memory, but it will never leave your application there without your variables.
+3


source


I actually set this up for Android 2.0 development. Perhaps better options are available now. What I did was to declare all actions to be global, and when you hit the home button from anywhere, we check to see if each one is null. If not null close them and set references to null. Actions are only declared globally. They are defined only when they are to be used.



0


source


If you are using Android 4.0 or later, you can enable "Don't take actions" in developer settings. If you don't have a 4.0 device, use an emulator.

0


source


you need to kill all actions that are at the top of the current activity on the home button and for this you need to override the master key functionality in which you have to write

Intent intent = new Intent(context,login.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

      

this will clear all actions at the top of that action when you press home key and switch it to login activity and

if you dont want to override Home key functionality then you only need to write one of Androidmanifest.xml which android: clearTaskOnLaunch = true , when ever you click on the app icon it will start your 1st activity.

I suggest you use the second approach.

0


source


For posterity, in my application, I just added finish (); into the onStop () method after everything I do there and it worked like a charm.

0


source







All Articles