Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear the Android Stack of activities?

I have an application with several Activities in Android and I want the user to be able to log-out by pressing a menu button. The problem I have is that

A) Android doesn't let you terminate the application and
B) even when I send the user to the LoginActivity again they can always press back and get right back to the previous activity they were in.

I already tried to launch the Activity with the two following flags:

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

I also tried with each one of them by themselves.

I also tried calling finish() after startActivity(intent) as I read in another StackOverflow question.

like image 231
Totic Avatar asked Nov 16 '10 01:11

Totic


People also ask

How do I delete a stack in activity?

In a case like that you can set the FLAG_ACTIVITY_CLEAR_TOP flag for the intent, meaning if the activity being launched is already running in the current task (LoginActivity), then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be ...

How do I start activity and clear back stack?

Declare Activity A as SingleTop by using [android:launchMode="singleTop"] in Android manifest. Now add the following flags while launching A from anywhere. It will clear the stack.

How can I see activity stack?

Just open the perspective Windows->Open Perspective-> Hierarchy View In the list you can see the all the connected devices and emulators and the activity stack. And in addition in the tree view you can see much more information about the view itself.


Video Answer


1 Answers

This should be bitwise OR'd or you end up overwriting the earlier flag.

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

Like so:

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); 
like image 150
deweylewie Avatar answered Sep 20 '22 08:09

deweylewie