Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Remove all the previous activities from the back stack

When i am clicking on Logout button in my Profile Activity i want to take user to Login page, where he needs to use new credentials.

Hence i used this code:

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

in the onButtonClick of the Logout button.

But the problem is when i click device back button on the Login Activity it takes me to the ProfileActivity. I was expecting the application should close when i press device back button on LoginActivity.

What am i doing wrong?

I also added android:launchMode="singleTop" in the manifest for my LoginActivity

Thank You

like image 618
Archie.bpgc Avatar asked Oct 18 '12 05:10

Archie.bpgc


People also ask

How do I delete all back activity on Android?

Use finishAffinity(); in task C when starting task A to clear backstack activities. Show activity on this post. Use finishAffinity() to clear all backstack with existing one.

How do I delete activity from stack?

The easiest way is to give the LoginActivity a “android:noHistory = true” attribute in the manifest file. That instructs Android to remove the given activity from the history stack thereby avoiding the aforementioned behavior altogether.

What is activity back stack?

These activities are arranged in a stack—the back stack—in the order in which each activity is opened. For example, an email app might have one activity to show a list of new messages. When the user selects a message, a new activity opens to view that message. This new activity is added to the back stack.


1 Answers

The solution proposed here worked for me:

Java

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); 

Kotlin

val i = Intent(this, NewActivity::class.java) // set the new task and clear flags i.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK startActivity(i) 

However, it requires API level >= 11.

like image 153
kgiannakakis Avatar answered Oct 29 '22 16:10

kgiannakakis