0

Currently I have 3 Activity classes A, B and C.

Activity A is a singleTask while other has the default launch mode. Consider a case: the user is first in A, then starts B, and then starts C.

The back stack now is ABC.

Next, the user starts A again.

The back stack now is A, but what I would like to be achieved is ABCA.

I know not setting Activity A to be singleTask can have a back stack : ABCA. But I really need the Activity A to be the same instance.

Anyone know how to do this?

Yash Sampat
  • 30,051
  • 12
  • 94
  • 120
Morty Choi
  • 373
  • 2
  • 10

3 Answers3

2

You have stipulated two conditions:

what I would like to be achieved is ABCA.

and

I really need the Activity A to be the same instance.

These two conditions are mutually contradictory. Absolutely.

What you want is impossible.

That is all.

Yash Sampat
  • 30,051
  • 12
  • 94
  • 120
0

That is not possible.

if you want to open existing activity then

launch your activity as

    Intent intent = new Intent(this, YourActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
    startActivity(intent);
shobhan
  • 1,460
  • 2
  • 14
  • 28
0

Are you sure you need singleTask and not singleTop ? Could you describe why do you need it singleTask ?

Ciprian
  • 2,879
  • 3
  • 28
  • 28
  • Because I want A to be a single instance. – Morty Choi May 27 '15 at 09:24
  • then maybe you should consider launching it with the same params and not make it singleTask, like @Y.S. said it's a bit contradictory what you want to achieve – Ciprian May 27 '15 at 09:26
  • Then consider the case of ABCA'BCA'', how can I jump back to A' or A immediately and clear the back stacks to ABCA' or A ? – Morty Choi May 27 '15 at 09:29
  • the answer for that would be CLEAR_TOP flag http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP – Ciprian May 27 '15 at 09:32
  • CLEAR_TOP need to be used together with startActivity(indent), but how can you specific which A or A' is being start and CLEAR_TOP? – Morty Choi May 27 '15 at 09:34
  • well, how else would you "jump"? – Ciprian May 27 '15 at 09:35
  • just call finish() on Resume of each Activity until I reach the targeted Activity? Or its is okay to store a reference to each activities? So that I can explicitly call finish() on those activities I would like to close. – Morty Choi May 27 '15 at 09:39
  • I wouldn't do that. It will lead to unexpected behavior. This is the reason why the intent flags were introduced to play around with the activity/task stack. You can find more info here: http://developer.android.com/guide/components/tasks-and-back-stack.html – Ciprian May 27 '15 at 09:42