Android kill all other activities on starting a new one
Asked Answered
S

5

5

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.

Sammie answered 6/12, 2013 at 9:57 Comment(1)
please use NO_HISTORY and manually do the backpress by overiding onBackpress meathod..I think that will solve your problemLangsyne
T
6

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);
Tradesfolk answered 6/12, 2013 at 10:9 Comment(5)
actually it seems there is no flag as Intent.FLAG_ACTIVITY_CLEAR_TASK maybe you meant this :Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET?Sammie
this is available from API level 11Tradesfolk
#11098779Tradesfolk
OP says minSdkVersion="8" in his post. He can't use Intent.FLAG_ACTIVITY_CLEAR_TASK. You need to read the question before you answer it.Marieann
My mistake. But the link I posted gives an alternave to this flagTradesfolk
S
3

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.

Swallowtailed answered 6/12, 2013 at 10:4 Comment(2)
your solution seems reasonable, this will work definetely. only more rows then settings a flag, thanks. probably I use this. I appreicateSammie
this works fine. very thank you. but I hope I can learn how to do it with flags some day.Sammie
M
1

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.

Marieann answered 6/12, 2013 at 17:38 Comment(0)
E
0

Use FLAG_ACTIVITY_CLEAR_TOP like this-

Intent loginIntent = new Intent(this, Login.class);
        loginIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(loginIntent);
    finish();
Equation answered 6/12, 2013 at 10:7 Comment(3)
I already tried this, but I also tried one more time after you wrote but it did not work. thanks.Sammie
Intent i = new Intent(SettingsActivity.this, StartupActivity.class); i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(i); finish();Sammie
This doesn't work because the Login activity is not in the task stack. He has already finished it. This would work if the Login activity were still at the root of the task stack.Marieann
B
0
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();
Baileybailie answered 28/3, 2015 at 7:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.