title: Android - how to kill all activities when HOME is pressed?
body: <p>I have an application containing multiple activities.</p>
  
  <p>At the moment the whole application contains about 8 activities. First I show a splash screen for a few seconds where all the preferences are loaded and set up (from sharedPreferences) - these are saved in a "Setting" class I made for this - this class basicly just have a bunch of static variables, so all activities in the app can read these and modify them as they need to. </p>
  
  <p><strong>EDIT</strong>: More data is getting stored in this class as the app runs, some of this is from a webservice - the data is parsed into obejcts and references to these obejcts are saved in the Settings class too (or a list of the objects).</p>
  
  <p>My problem is then, that when users press the HOME key, the current activity is put in the background. If the activity is in the background for a long time (a lot of users "close" apps by pressing home instead of back), and then reopened it shows the activity that was running before HOME was pressed. </p>
  
  <p>As an example - lets say the user starts the app, sees the Splash screen for a few seconds. The splash screen then starts a new activity and calls finish on itself. </p>
  
  <p>This means that now the Activity stack is just the MainActivity (main menu). In the MainActivity I supply all the buttons with listeners in the onCreate method, and most buttons require some info from the Settings class mentioned above.</p>
  
  <p>When I then press HOME and reopens the app a few hours later, none of the buttons works anymore - seems the graphic is reacting etc, but something still goes wrong. One of the buttons that should work even with all settings wiped will just open a dialog with some text in it.</p>
  
  <p>The listener:</p>
  
  <pre><code>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();
    }
  });
  </code></pre>
  
  <p>I have some constantst for dialog types, as I'm using some custom designs for the dialogs I have created a class to created the dialogs for me.</p>
  
  <p>From the activity containing the button (simplified a bit):</p>
  
  <pre><code>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;
  }
  </code></pre>
  
  <p>The CustomDialog class extends the Dialog class and then depending on the const it
  s supplied with it inflates one of several dialog layouts and adds content etc. Works great normally, but after the app have been paused by HOME for a while, and things go wrong no dialog is shown when I press the button. I do se a flash of a loading dialog on the other buttons, but nothing happens afterwards - no dialog is shown. Strange thing is that if I press on the button again in the middle of the screen, I can't press it (grahics dont react), but when pressing once in the side of the screen and then in the middle the graphics do react, the dialogs does get cancelled when pressing outside, so I'm wondering if there is some very slim "transparent" dialog in the middle or something - I just have no clue why this would happen.</p>
  
  <p><strong>EDIT</strong>: Actually looking through the variables I save in Settings class I see that I do save the width and height in pixels of the screen. Width used to set the width of the custom dialogs to a certain % of the screen width. IF the Settings gets wiped, that might explain why I don't see any dialogs as width is set to 0... I really can't seem to figure what is happening if it's not all my variables in the Settings class that gets wiped - everything really do point to that.</p>
  
  <pre><code>dialog.getWindow().setLayout(Settings._widthpx - (Settings._widthpx/5), dialog.getWindow().getAttributes().height); //Width = 80%
  </code></pre>
  
  <p>Actually I have to admit that I don't really know what is causing this, as I'm rather new to Android. I suspect that the GC does delete all my variables after a while when the Settings class haven't been used - causing all settings to be wiped when the user returns a few hours later. This however does not explain why the buttons doesn't work (one of them require no setting).</p>
  
  <p>This main activity can start other activities, and these can again launch new activities. </p>
  
  <p>I think all my problems can be solved if I can just force the whole app to close when HOME is pressed, and thereby force it to be started from scratch whenever the icon is pressed - forcing the listeners on the buttons and the settings to be loaded.</p>
  
  <p>I read on here about "android:clearTaskOnLaunch", " android:launchMode" and "android:finishOnTaskLaunch" but I'm not quite sure how to use these correct.</p>
  
  <p>Anyone who could either explain to me why the buttons does not work, or what might happen to the variables in my Settings class when the app has been in the background for a while, or maybe give me a few good hints on how to use the "activity" settigns properly.</p>
  
  <p><strong>EDIT</strong>: The app will be running Android 1.6+, so I can't use any newer functions or anything...</p>
  
  <p>Thank you very much</p>