Android: Clear Activity Stack
Most of you are wrong. If you want to close existing activity stack regardless of what's in there and create new root, correct set of flags is the following:
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
From the doc:
public static final int FLAG_ACTIVITY_CLEAR_TASK
Added in API level 11If set in an Intent passed to
Context.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 withFLAG_ACTIVITY_NEW_TASK
.
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.
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.
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 Activity Stack and start new activity in android
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...
Related Topics
How to Set Android Tablayout in The Bottom of The Screen
Retrofit and Get Using Parameters
Access The Sim Card with an Android Application
To Run Dex in Process, The Gradle Daemon Needs a Larger Heap. It Currently Has Approximately 910 Mb
How to Set Status Bar Background as Gradient Color or a Drawable in Android
Get All Subscribed Topics from Firebase Cloud Messaging
Set Textcursordrawable Programmatically
How to Repeat Notification Daily on Specific Time in Android Through Background Service
How to Create a Persistent Alarmmanager
Handle Screen Orientation Changes When There Are Asynctasks Running
How to Pause Android.Speech.Tts.Texttospeech
Crop Square Image to Circle - Programmatically
Zipalign - Command Not Found - MAC Terminal
Set View Text Align at Center in Spinner in Android