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
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.
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.
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.
The solution proposed here worked for me:
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);
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.
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