Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Clear Activity Stack Below An Activity

in my application, when I start a specific activity I want all the activities in the same package to be cleared from the stack underneath. Could someone help me on how to do this? Also I do not want to use android:noHistory="true" in the manifest because I only want the stack history to be cleared on starting this specific activity.

EDIT:

To make my point more clear, suppose I have activity a. From a I start activity b. From b I start c. But when I start c I want to clear b and a.

like image 209
Tanuj Nayak Avatar asked Jan 02 '13 00:01

Tanuj Nayak


People also ask

What is an activity stack in Android?

A task is a collection of activities that users interact with when trying to do something in your app. 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.


4 Answers

Oh guys, I figured out that you just have to put the following code with the Intent which starts the stack clearing activity:

Intent i = new Intent(this,MyActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);

Thanks for your help though.

like image 69
Tanuj Nayak Avatar answered Oct 05 '22 12:10

Tanuj Nayak


Try this,

Add android:launchMode="singleTop" to the your Specific Activity that wanted to clear all the stacked activity.

Then use intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) and intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) when starting your Specific Activity.

Source: Android: Clear the back stack

like image 24
IssacZH. Avatar answered Oct 05 '22 12:10

IssacZH.


Intent intent = new Intent(getApplicationContext(), YOUR_CLASS.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
like image 25
Kevin Antonio Avatar answered Oct 05 '22 12:10

Kevin Antonio


Set flag before the activity is started...whats the point of setting the flag after starting the activity....the code should look something like this,

Intent intent = new Intent(getContext(), ClassName.class);
intent.setFlags( Intent.FLAG_ACTIVITY_CLEAR_TOP );
  v.getRootView().getContext().startActivity(intent);

  removeSessionFiles();
like image 45
Puneet Avatar answered Oct 05 '22 10:10

Puneet