14

My question is little bit different than these type of question. I need to remove or clear my activity stack then start a new activity. I don't think it is a clear_top flag problem. I am explaining with an example:

My Activity flow :

Login > Home > Screen1 > screen2 ....

I finish Login activity or call with no_history flag. So my activities are look like this

Login(finished)> Home [bottom of the stack now] > Screen1 > Screen2[top of the stack]

I need to handle session error. If any session error occurs in any point i need to go back to login activity. But remember i don't have login activity in stack. So clear_top will not work.

If any session error occurs in Screen2 then i need to clear full stack (screen2, screen1, home) and then start login activity. So that after back button press in login activity will close my apps.

Is there any way to clear the activity stack?

thanks in advance

shantanu
  • 2,408
  • 2
  • 26
  • 56
  • Now the question is, do you want to return to login activity, even if it is not in the stack, then wyh are you finishing that activity. Second question is in what case, you want to clear the stack and return back. Thirs one is what you want to clarify with _So that after back button press in login activity will close my apps_ . – Sahil Mahajan Mj Sep 13 '12 at 10:01
  • First: I have an option for save session. If i login successfully then it opens HOME activity. According to flow, when we press back button from HOME activity it should close the apps. Only sign out or session error can show login page. Second: Think a website. What happen when your session expired? It redirect your to login page. I want to do exactly the same. I have to close all my activity which need login permission on session expired. – shantanu Sep 14 '12 at 03:53
  • Possible duplicate of [Android: Clear Activity Stack](https://stackoverflow.com/questions/7075349/android-clear-activity-stack) – Mehdi Dehghani May 19 '18 at 12:11

4 Answers4

21

Use this

Intent i = new Intent(yourScreen.this,Home.class);
        i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        i.putExtra("EXIT", true);
        startActivity(i);

and in the onCreate of the Home class, do this to check,

if (getIntent().getBooleanExtra("EXIT", false)) 
    {
        Intent i = new Intent(Home.this,Login.class);
        startActivity(i);
        finish();
    }

what this will essentially do is no matter at what activity you are, you can call the home screen with the clear top flag. In the home screen there is a check condition in the onCreate method which will help to clear the stack and take you to the login screen.. Now on the login screen,if you press back button you will exit the app as the stack is cleared..

Let me know if the problem still persists...

Gautam Mandsorwale
  • 1,580
  • 1
  • 18
  • 27
11

It's bit old question, but maybe someone else will stumble upon it while searching answer to similar problem.

You should start Login activity with flags: Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK - flag NEW_TASK may have a little bit confusing name, but it will actually create new task just if it doesn't exist (otherwise current task will be used) - and CLEAR_TASK will clear it from all previous activities.

blackpanther
  • 10,998
  • 11
  • 48
  • 78
AntekM
  • 111
  • 1
  • 3
2

Try this,

Finish your current Activity

     YourCurrentActivity.this.finish();  
     Intent intent1 = new Intent(YourCurrentActivity.this,LoginActivity.class);  
     intent1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);   
     startActivity(intent1); 

It will work even your activity is not in the stack.

Hope it helps.

vinothp
  • 9,939
  • 19
  • 61
  • 103
  • doesn't work in this flow. A (finish) -> B -> C (do logout) -> A. Pressing back on A will still go back to B. – topwik Nov 30 '12 at 18:04
0

Use onActivityResult() to manage activities in this scenario.

ankita gahoi
  • 1,532
  • 2
  • 15
  • 28