4

after looking up the 'Defining launch modes' section of the 'Tasks and Back Stack' guide , I dont understand exactly the behavior of an activity declared with "singleTask".

assume that I start a singleTask-activity (via startActivity method ) when there is an instance of that activity laying at the bottom(root activity) of a background task,

as the article describes, the Intent I use to start singleTask-activity will be delivered to the instance, in this case, does the instance bring itself to top of its task and its task to foreground, or just bring its task to foreground without bringing itself to top of its task?

Do I make myself clear? pls help! thanks in advance

user718146
  • 400
  • 1
  • 4
  • 16
  • I would like to thank people, how do I do that? by voting? – user718146 Jun 08 '11 at 01:22
  • you must go back to previous questions you've asked and accept answers that solved your problem. currently you've asked 8 questions and haven't accepted a single answer. – Will Tate Jun 08 '11 at 02:06
  • You may look at this aswer http://stackoverflow.com/questions/2417468/android-bug-in-launchmode-singletask-activity-stack-not-preserved – tantra35 Apr 13 '13 at 05:27

3 Answers3

8

I was/am having the same question actually. After reading replies here on stackoverflow and doing some experiments, I believe that when a singleTask activity is launched while it's already in a background task, the system will kill all other activities at the top of the stack and resume the singleTask activity.

For sure, this is not what I got out of the documentation though.

Here is my theory:

Launch Modes: Activity A: singleTask , Activity B: standard

experiment 1

  • Launch A ; stack: [A]
  • Launch B from A; stack: [A|B]
  • Press home; stack: [A|B]
  • Launch A from launcher; Stack: [A] (onDestroy was called on B before onResume() on A)

experiment 2

  • Launch A ; stack: [A]
  • Launch B from A; stack: [A|B]
  • Launch A from B; stack: [A] (onDestroy was called on B after onResume() on A)

In my case, I had to use android:alwaysRetainTaskState="true" although this is not ideal as I do want the stack to be cleared after, say, 10 minutes as this recommendation makes sense:

If the user leaves a task for a long time, the system clears the task of all activities except the root activity. When the user returns to the task again, only the root activity is restored. The system behaves this way, because, after an extended amount of time, users likely have abandoned what they were doing before and are returning to the task to begin something new.

from the Tasks and Back Stack guide.

I hope this helps other people.

Gema Sanchez
  • 1,106
  • 7
  • 6
dimi
  • 1,496
  • 2
  • 15
  • 27
0

Then how does FLAG_ACTIVITY_CLEAR_TOP is different from Single Task?

Is the difference only that FLAG_ACTIVITY_CLEAR_TOP is a subset of single task, which only works the same way as but only if the activity being invoked is part of the current task?

Shilpi
  • 498
  • 1
  • 6
  • 13
0

If the Activity already exists it will call onNewIntent() for that Activity. Since an Activity is always paused before receiving a new Intent, onResume() will be called shortly after onNewIntent() is received. The fact onResume() is called means the Activity will come to the foreground and be visible to the user.

Will Tate
  • 33,439
  • 9
  • 77
  • 71
  • thanks for reply, now it is sure that the existing instance will come to foreground, but, how does it come to foreground? popping out those activities on its top until itself on the top of that task? or other method? – user718146 Jun 08 '11 at 01:19
  • the documentation doesn't seem to be specific about how it comes to the top of the stack. it only guarantees that if `onNewIntent()` is called then `onResume()` will be called and the `Activity` we are dealing with we come to the foreground. – Will Tate Jun 08 '11 at 02:05