6

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.

Tanuj Nayak
  • 607
  • 1
  • 8
  • 22
  • 1
    for every activity recycling you can use finish() after every intent passing. and the second way is FLAG_ACTIVITY_CLEAR_TOP,FLAG_ACTIVITY_NEW_TASK and then also you can use intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY . hope this will help you. – itsrajesh4uguys Jan 02 '13 at 06:21
  • I'm sorry but if you read the question more closely you will observe that I mentioned that I want the stack history to be cleared on starting **this** specific activity not any other activity. So if I finish every activity on pausing, then it won't be there when I want them to. I just don't want the stack to be there on starting the specific activity I mentioned because it is a transparent activity. – Tanuj Nayak Jan 02 '13 at 06:24
  • for that use Intent.FLAG_ACTIVITY_NO_HISTORY before calling the specific activity you want . – itsrajesh4uguys Jan 02 '13 at 06:26
  • kindly check the attached stackoverflow link in my answer. – IssacZH. Jan 02 '13 at 06:28

5 Answers5

4

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.

Tanuj Nayak
  • 607
  • 1
  • 8
  • 22
  • That's exactly what I mentioned in my answer. Glad you found it anyway. – IssacZH. Jan 03 '13 at 01:27
  • 2
    Im sorry but my answer has i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK) not Intent.FLAG_ACTIVITY_CLEAR_TOP. Thanks for your commitment though. – Tanuj Nayak Jan 04 '13 at 05:34
1

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

Community
  • 1
  • 1
IssacZH.
  • 1,457
  • 3
  • 24
  • 54
1
Intent intent = new Intent(getApplicationContext(), YOUR_CLASS.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
Kevin Antonio
  • 578
  • 3
  • 13
1

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();
Puneet
  • 611
  • 6
  • 22
  • Actually this code isn't in an onClick method. It is in an onReceive method of a BroadcastReceiver in a service. – Tanuj Nayak Jan 02 '13 at 06:19
  • 1
    May be you can try something like this Suppose in our application, we have a number of activities(say ten) and we need to exit directly from this activity. What we can do is, create an intent and go to the root activity and set flag in the intent as 'intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); also, add some extra like boolean to the intent intent.putExtra("EXIT", true); Then in root activity, check the value of the boolean and according to that call finish(), in the onCreate() of the root activity if (getIntent().getBooleanExtra("EXIT", false)) { finish(); }' – Puneet Jan 02 '13 at 06:38
0

I know this is an old thread but there I would like to share the syntax in kotlin with anyone who would like to know.

 val i = Intent(this@PresentActivity,NextActivity::class.java)
     i.flags = Intent.FLAG_ACTIVITY_CLEAR_TASK and Intent.FLAG_ACTIVITY_NEW_TASK
     startActivity(i)

EDIT

Using kotlin let

Intent(this@PresentActivity,NextActivity::class.java).let { intent -> intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TASK and Intent.FLAG_ACTIVITY_NEW_TASK startActivity(intent) }

chinedu
  • 1
  • 1