-3

I need to clear the activity stack. All the activity except the current one in SDK 8. I want after logout from any activity I can reach to the Login Activity and All previous Activity will get Removed from the activity stack. I have tried this solution

 Intent i = new Intent(BaseActivity.this, LoginActivity.class);
        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        startActivity(i);

But it is not working. I can't use finish() because user can logout from any activity.

Your Help is Highly appreciable.

MrDumb
  • 2,140
  • 1
  • 21
  • 31
  • Intent i = new Intent(BaseActivity.this, LoginActivity.class); i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(i); I am using this code. It should start LoginActivity and Clear the stacktrace but instead I can go to the previous activities. – MrDumb Jul 31 '14 at 11:00

3 Answers3

0

Just use this flag on its own Intent. FLAG_ACTIVITY_CLEAR_TASK

Simon
  • 10,932
  • 50
  • 49
  • This flag supports SDK level 11 I need for SDK 8. [link](http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TASK) – MrDumb Jul 31 '14 at 11:30
0

Use This Normally when we launch new activity, it’s previous activities will be kept in a queue like a stack of activities. So if you want to kill all the previous activities, just follow these methods

Intent i = new Intent(OldActivity.this, NewActivity.class); // set the new task and clear flags i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK) startActivity(i);

Guru
  • 186
  • 14
  • This flag supports SDK level 11, I needed it for SDK 8, Please check this [link](http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TASK). Anyways I have solve this problem. I have posted my answer too. – MrDumb Aug 01 '14 at 06:11
-1

I have got two Beautiful solution of my problem.

SOLUTION 1:

One is by using Broadcast Receiver. That I can broadcast a message to all my activity and onRecive() I can finish all my activity. As this Link says.

SOLUTION 2 (A Much Easier way):

Second is by managing a flag in ShearedPrefrences or in Application Activity. In starting of app (on splash screen) set the flag = false; OnLogout click event, just set the flag true and in OnResume() of every activity check if flag is true then call finish().

It works like a charm :) And please If you can't answer at least don't abuse the question.

Community
  • 1
  • 1
MrDumb
  • 2,140
  • 1
  • 21
  • 31