Android: Clear the back stack
Try adding FLAG_ACTIVITY_NEW_TASK
as described in the docs for FLAG_ACTIVITY_CLEAR_TOP
:
This launch mode can also be used to
good effect in conjunction with
FLAG_ACTIVITY_NEW_TASK: if used to
start the root activity of a task, it
will bring any currently running
instance of that task to the
foreground, and then clear it to its
root state. This is especially useful,
for example, when launching an
activity from the notification
manager.
So your code to launch A
would be:
Intent intent = new Intent(this, A.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
CurrentActivity.this.finish(); // if the activity running has it's own context
// view.getContext().finish() for fragments etc.
Android: Remove all the previous activities from the back stack
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.
Clear the entire history stack and start a new activity on Android
In API level 11 a new Intent Flag was added just for this: Intent.FLAG_ACTIVITY_CLEAR_TASK
Just to clarify, use this:
Java
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
Kotlin
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
Unfortunately for API lvl <= 10, I haven't yet found a clean solution to this.
The "DontHackAndroidLikeThis" solution is indeed pure hackery. You should not do that. :)
Edit:
As per @Ben Pearson's comment, for API <=10 now one can use IntentCompat class for the same. One can use IntentCompat.FLAG_ACTIVITY_CLEAR_TASK
flag to clear task. So you can support pre API level 11 as well.
Android clear back stack between activities
If the activity being launched is already running in the current task, 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 delivered to the (now on top) old activity as a new Intent.
Intent intent = new Intent(this, ActivityA.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
Read more here.
Clear ALL activities in back stack
Well, i ended up using this answer
Clear the entire history stack and start a new activity on Android
And that do exactly what Im trying to do
Android clear backstack after reselecting Bottom Navigation tab
BottomNavigationView
has its own method for handling reselection via setOnItemReselectedListener()
(or, when using an earlier version of the Material Design Library, the now deprecated setOnNavigationItemReselectedListener()
).
bottomNavigationView.setupWithNavController
does not set this listener (as there is no Material specification for exactly what reselecting a tab should do), so you need to set it yourself:
val bottomNavigationView = findViewById<BottomNavigationView>(R.id.bottom_nav)
bottomNavigationView.setupWithNavController(navController)
// Add your own reselected listener
bottomNavigationView.setOnItemReselectedListener { item ->
// Pop everything up to the reselected item
val reselectedDestinationId = item.itemId
navController.popBackStack(reselectedDestinationId, inclusive = false)
}
Related Topics
Notification Bar Icon Turns White in Android 5 Lollipop
Getting the Screen Density Programmatically in Android
Difference Between Getcontext() , Getapplicationcontext() , Getbasecontext() and "This"
Android Action_Image_Capture Intent
How to Change Fontfamily of Textview in Android
How to Run a Runnable Thread in Android At Defined Intervals
Saving and Reading Bitmaps/Images from Internal Memory in Android
"R Cannot Be Resolved to a Variable"
Getting the Absolute File Path from Content Uri For Searched Images
Can the Android Layout Folder Contain Subfolders
Re-Launch of Activity on Home Button, But...Only the First Time
Service VS Intentservice in the Android Platform
How to Handle Button Clicks Using the Xml Onclick Within Fragments
"Failed to Load Ad: 3" With Doubleclick
Different Values Folders in Android
Java.Util.Zip.Zipexception: Duplicate Entry During Packagealldebugclassesformultidex