Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android kill all other activities on starting a new one

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.

like image 608
anzaidemirzoi Avatar asked Dec 06 '13 09:12

anzaidemirzoi


People also ask

How do I close all activities on Android?

exit(); or finish(); it exit full application or all activity.

When can Android activity be destroyed?

The Activity lifecycle is especially important because whenever an activity leaves the screen, the activity can be destroyed. When an activity is destroyed, when the user returns to the activity, the activity will be re-created and the lifecycle methods will be called again.


1 Answers

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);
like image 116
vipul mittal Avatar answered Sep 25 '22 13:09

vipul mittal