5

In android I have the following path:

Activity 1 -> Activity 2 -> Activity 3 -> ... Activity N -> press button ...

When the button is pressed I want to Clear/Finish ALL activities from Activity 2 until N and then go to Acitivy X. In other words I want to finish all activities down to the initial One and then move to another.

If I use the flags:

CLEAR_TOP, CLEAR_TASK, NEW_TASK etc

theoritically it would finish ALL previous activities with the initial one. Is there any way to keep the initial one alive and move to activity X?

Panos
  • 7,227
  • 13
  • 60
  • 95
  • Hi, did you try to use FLAG_ACTIVITY_REORDER_TO_FRONT ? It will bring the running instance(if there's one) of your activity to the top of the stack, then you can clear all the orders and continue... this could be a solution don't know if it will works for you. Also, check this one: http://stackoverflow.com/questions/2424488/android-new-intent-starts-new-instance-with-androidlaunchmode-singletop – adheus Jun 03 '14 at 22:47

2 Answers2

2

I'm not sure I understand the question. Or rather, the reason for not being able to use FLAG_ACTIVITY_CLEAR_TOP.

If set, and 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.

For example, consider a task consisting of the activities: A, B, C, D. If D calls startActivity() with an Intent that resolves to the component of activity B, then C and D will be finished and B receive the given Intent, resulting in the stack now being: A, B.

If I understand correctly, that's exactly what you want. In particular for,

Activity 1 -> Activity 2 -> Activity 3 -> ... Activity N -> press button ...

Launching an Intent with Activity2.class and flags FLAG_ACTIVITY_CLEAR_TOP | FLAG_ACTIVITY_SINGLE_TOP would go back to the existing instance of Activity2 (instead of creating a new one) and deliver the new intent in onNewIntent(). You just need to add an extra to this intent, so that this method will know that it's supposed to call ActivityX afterwards.

That is, unless I'm missing something. :)

matiash
  • 54,791
  • 16
  • 125
  • 154
  • Hmmm. That I think is a good Idea. So I will have to start acitivity 1 with clear_top and pass an extra to tell it to move to Activity X after it starts. Although the user will have to wait for both acitivties to load. The other problem is that the first activity must know all "extras" of acitivity X and pass them back – Panos Jun 04 '14 at 08:42
  • @Panos The user won't actually have to wait for Activity1 (I thought it was 2) if you pass in single_top, the existing one will be brought back to the front. And it doesn't need to know all extras either, you can just use `intent.putExtras(src)` in `onNewIntent()` to copy them all. – matiash Jun 04 '14 at 14:25
1

if you have already visited a actitvity and have not called finish() on leaving it then do this

finish();
Intent mIntent = new Intent(Create_Your_Pizza.this, MainActivity.class);
mIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);;

mIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(mIntent);
rafaelc
  • 57,686
  • 15
  • 58
  • 82
Passion
  • 662
  • 1
  • 11
  • 29