Removing an Activity from the History Stack

How to remove activity from back stack in android?

In Activity A you can finish all activities in onResume for that you need to store all activity's this in staic variable

Activity B

public static B b;
oncreate(){
//...
b=this;
}

Do the same for C and D and in A

onResume(){
clearAllTasks();
}

public void clearAllTasks(){
if(B.b!=null){
B.b.finish();
}
if(C.c!=null){
C.c.finish();
}
if(D.d!=null){
D.d.finish();
}
}

in manifest

<application>
<activity android:name="A"
android:noHistory="true">
</activity>
//...
</application>

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.

How remove last activity from the stack?

There are a few ways to do this.


If you know which activity you don't want to remove from the stack, all you have to do is call finish() after your call startActivity(intent) to the next activity, so this activity will be excluded from the flow, see the example below:

Intent intent = new Intent(...);
startActivity(intent);
finish();

If the decision will happen based on an interaction with the user, you can call startActivityForResult() to start the next activity, and when the activity ends, you have to return a RESULT value.

Here's a good documentation about how to use startActivityForResult():
http://developer.android.com/training/basics/intents/result.html

How to clear specific activity from the stack history?

I'm not sure you can directly programmatically remove activities from the history, but if you use startActivityForResult() instead of startActivity(), then depending on the return value from your activity, you can then immediately finish() the predecessor activity to simulate the behaviour you want. By using this method in all your activities, you can have this behaviour cascading the activity stack to allow you to go from activity D to activity A.

I know this isn't your situation, but in future if you know before you start the activity that you don't want the predecessor to remain, you can call finish() immediately after startActivity().

Please see the section called "Lifetime of the New Screen" in Common Tasks and How to do Them in Android

Remove start-activity from the history

You should use FLAG_ACTIVITY_NO_HISTORY when starting StartActivity to achieve described behaviour.

The same you may achieve if you set noHistory attribute to true in your AndroidManifest.xml.

Android remove Activity from back stack

FLAG_ACTIVITY_CLEAR_TOP clears your Activity stack , you can use the code below:

Intent intent = new Intent(this, Activity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

Remember that this flag clears just Intermediate Activities , for example if you have A,B,C in your Back Stack then going from C Activity to D with this flag this does not clear Back Stack and the Stack would be A,B,C,D but if you go from Activity D to Activity A with this flag , B,C,D Activities will pop up from the stack and you will have just A in the Back Stack .



Related Topics



Leave a reply



Submit