Android kill all other activities on starting a new one

6.5k Views Asked by At

I have an android activity problem.

Here is how my process works:

  1. Login Activity starts
  2. Login successful. MainMenuActivity starts and LoginActivity is finished by me.
  3. User touched on settings and SettingsActivity starts. MainMenuActivity is NOT finished. because is it the main menu. when user presses the back on settings screen I need to go back MainMenuActivity. so I cant kill MainMenu.
  4. User touched on log out and SettingsActivity is finished by me and Login activity starts. As user returns the login I need to kill MainMenuActivity but I cant.:/
  5. I tried FLAG_ACTIVITY_SINGLE_TOP, CLEAR_TOP, SINGLE_TASK, NEW_TASK, NO_HISTORY etc.. almost all of them didnt work
  6. I put launchMode="singleTask", clearTaskOnLaunh="true" etc. didtn work again.
  7. I tried addFlags() and setFlags() both, didnt work
  8. There are some many issues about this topic here, I read and applied all the suggested solutions and didnt work.

Can anyone help, please?

P.S android:minSdkVersion="8" and android:targetSdkVersion="15" for my app. I didnt use fragments in the app, I use old activity structure.

5

There are 5 best solutions below

0
On

I suggest that you don't finish the Login activity when you start the main menu. Then you can always clear all activities on logout by doing this:

    Intent intent = new Intent(this, Login.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(intent);

This will only work if Login activity is still active at the root (beginning) of the task stack.

To prevent the user from BACKing into the Login activity from the Main activity, you can override onBackPressed() in Main activity and do something else.

2
On

I'd suggest to start the settings activity for result (see here), and when the user requests to logout, set a result accordingly. You will get this result in MainActivity's onActivityResult (see here) and can handle the logout there, finishing the mainActivity before starting the loginActivity.

3
On

Use FLAG_ACTIVITY_CLEAR_TOP like this-

Intent loginIntent = new Intent(this, Login.class);
        loginIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(loginIntent);
    finish();
5
On

Use the combination of two flags like this:

Intent intent = new Intent(this, Login.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);
        finish();

This deletes all the other activities and starts this one.

Try this.

For api level <11

i.addFlag(Intent.FLAG_ACTIVITY_NO_HISTORY | 
              Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
0
On
Intent intent = new Intent(MainActivity.this, MyActivity2.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                    | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                    startActivity(intent);
                    finish();
  1. in this example Mainactivity automatically closed and Myactivity2 Start. and one thing you need to add finish();