0

I have an app that will have multiple activities open.

Activity A -> Activity B -> Activity C

I want to logout and close all open activities. I have read many different links that describes how to do this.

On logout, clear Activity history stack, preventing "back" button from opening logged-in-only Activites

Close all running activities in an android application?

The issue for this however is that the Intent Flags only work with API level 11 and above. I have an app that I would like to be made available for API level 8 and above. I know that it goes way back but what is the best way to accomplish this for API level 8?

Or should I just give in and make the minimum level 11?

Community
  • 1
  • 1
thomas
  • 91
  • 1
  • 8

4 Answers4

2

Intent intent = new Intent(this, LoginActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent);``

Abhijit S
  • 181
  • 1
  • 7
0

Did you try this ?

   void signOut() {
        Intent intent = new Intent(this, HomeActivity.class);
        intent.putExtra("finish", true);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // To clean up all activities *not necessary*
        startActivity(intent);
        finish();
   }

from this answer

Community
  • 1
  • 1
Ahmed Ekri
  • 4,601
  • 3
  • 23
  • 42
0

Okay, you can do one thing get ProcessID of your application and in onDestroy() kill that process. Thats it from this link How to force stop my android application programmatically?

int pid=android.os.Process.myPid();
android.os.Process.killProcess(pid);

you can use alternative told by one of user here using startActivityForResult() and onActivityResult() works fine even on API level 8.

For this u need to use

 startActivityForResult() instead of startActivty. and onActivtyResult() for you work.
Community
  • 1
  • 1
Manmohan Badaya
  • 2,326
  • 1
  • 22
  • 35
0

you say "I want to logout and close all open activities." , Intent.FLAG_ACTIVITY_CLEAR_TOP works on API Level 8 , I have incorporated Logout functionality ,that way in app that is already in store, but there is one difference if I understand you correctly , logout goes to LoginActivity , logout does not kill process or something as this goes against concept of Android. Anyway you can even do this also when you do this as suggested above:

  void logout() {
        Intent intent = new Intent(this, LoginActivity.class);
        intent.putExtra("finish", true);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
   }

And then in LoginActivity you can search for "finish" boolean in extra in onCreate method and finish that activity too.

@Override public void onCreate(Bundle state) {
    super.onCreate(state);
    if (getIntent().getBooleanExtra("finish", false)) finish();
}
Renetik
  • 5,887
  • 1
  • 47
  • 66