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: 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...
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 Activity history tracking and clearing partially
This is all pretty standard. Don't use any special launch modes. Normally, pressing BACK will just finish the current Activity
and drop you back into the previous one.
For this case:
If the user is on E then on tapping a custom button "Show B", then it
should clear E > D > C. Which is similar to Finish().
In E
, to go back to the existing instance of B
, do this:
Intent intent = new Intent(this, B.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
This will finish E, D and C and return to the existing instance of B.
The flag FLAG_ACTIVITY_CLEAR_TOP
tells Android to clear all activities between the current Activity
and the target Activity
. If you don't specify FLAG_ACTIVITY_SINGLE_TOP
then the existing instance of the target Activity
will also be finished and a new instance will be created. If you do specify FLAG_ACTIVITY_SINGLE_TOP
then the existing instance of the target Activity
will NOT be finished and a new instance will NOT be created.
Clear Activities history stack
you can Do a Trick for this Question. I have Used it and works fine with me.
Write below Line of Code inside your ThirdActivity onclick.
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(ThirdActivity.this,FirstActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("GO", false);
startActivity(intent);
finish();
}
});
After Write Below Code inside your OnCreate of FirstActivity Class:
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
// Getting the Value of GO
GO = getIntent().getExtras().getBoolean("GO");
if(GO){
setContentView(R.layout.form_data);
...
// Here your Code for this Activity
...
}else{
Intent intent=new Intent(FirstActivity.this,MainActivity.class);
startActivity(intent);
finish();
}
}
Also put Extra value of GO as true while calling FirstActivity from MainActivity as Below
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this,FirstActivity.class);
intent.putExtra("GO", true);
startActivity(intent);
finish();
}
});
Hope it will Solve your Problem.
Keep the root activity even after starting new activity and clearing the back stack
You can acheive that with TaskStackBuilder. This dude lets you rebuild stack which you need. You need somethink like this:
final Intent activityAIntent = new Intent(this, ActivityA.class);
activityAIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
TaskStackBuilder.create(this)
.addNextIntent(activityAIntent)
.addNextIntent(new Intent(this, ActivityE.class))
.startActivities();
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
Related Topics
Android Studio Keeps Refusing to Resolve Com.Android.Support:Appcompat-V7:29.0.1
How to Refresh App Upon Shaking the Device
Android Error: Failed to Install *.Apk on Device *: Timeout
How to Change Colors of a Drawable in Android
Android: How to Have Viewpager Wrap_Content
How to Change the Text on the Action Bar
Intercepting Links from the Browser to Open My Android App
Android Create Shortcuts on the Home Screen
How to Get Pixel Color in Android
Android Drawing Separator/Divider Line in Layout
Separate Back Stack For Each Tab in Android Using Fragments
How to Get the Absolute Coordinates of a View
Java.Lang.Runtimeexception: Unable to Instantiate Activity Componentinfo
Runtimeexception: Your Content Must Have a Listview Whose Id Attribute Is 'Android.R.Id.List'
Onitemclicklistener Not Working in Listview