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
startActivity() , this flag will cause any existing task that would be associated with the activity to be cleared before the activity is started. That is, the activity becomes the new root of an otherwise empty task, and any old activities are finished. This can only be used in conjunction with FLAG_ACTIVITY_NEW_TASK .
If you just want to reload the activity, for whatever reason, you can use this. recreate(); where this is the Activity. This is never a good practice. Instead you should startActivity() for the same activity and call finish() in the current one.
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...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With