37

I read the android developer guide and some articles in internet, I'm still confusing about the singleTask launchmode. Lets take an example:

User launch the App1, android starts a new task. Assume the App1 creates activities in follow order:

ActivityA -> ActivityB -> ActivityC

That's how task1 looks like.

Then user click the home buttom and choose to launch App2, so task1 goes in background and android start a new task: task2, user does something:

ActivityD -> ActivityE

now lets say ActivityE try to start ActivityB , and ActivityB has the launchmode singleTask.

What I understand is that task1 comes to frontend again and task2 goes to background. And task1 looks now like this:

ActivityA -> ActivityB

Which means:

  1. The ActivityC will be removed from task1 and ActivityB becomes to the top Activity.

  2. If user now click on "Back" button, he will come to ActivityA of task1 instead of back to ActivityE of task2

Am I right?

Thanks

cn1h
  • 1,188
  • 4
  • 16
  • 24

3 Answers3

20

You sound right.

Why don't you test it.

There is also this app that can help explain launch mode:

https://play.google.com/store/apps/details?id=com.novoda.demos.activitylaunchmode

enter image description here

Sources are at https://github.com/gnorsilva/Activities-LaunchMode-demo

Paul Verest
  • 60,022
  • 51
  • 208
  • 332
Blundell
  • 75,855
  • 30
  • 208
  • 233
  • 2
    thanks, but what im confusing is this document: http://developer.android.com/guide/topics/manifest/activity-element.html#lmode It says: In contrast, "singleTask" and "singleInstance" activities can only begin a task. They are always at the root of the activity stack. – cn1h Jul 26 '12 at 12:22
  • 2
    there is a mistake in the demo.When you start a new activity whose languch-mode is 'singleTask',the activity will always be created in a new task.But actually,It only will be created in a new task if you set **taskAffinity** for the activity. – smileVann Nov 24 '13 at 07:12
10
The ActivityC will be removed from task1 and ActivityB becomes the top Activity.

Yes, you are Right...

ActivityC will be removed from i.e. the onDestroy method of the ActivityC will be called. Hence when the user launches Task 1 again, the ActivityB is shown rather than ActivityC.

Have created 2 Tasks (Projects) and uploaded the same @ SendSpace. Try it out...

If you look at androids documentation it says

" A "singleTask" activity allows other activities to be part of its task. It's always at the root of its task, but other activities (necessarily "standard" and "singleTop" activities) can be launched into that task."

This means that when you click the home button all the activities above the single-task activity (which in your case is ActivityB) are removed from the stack.

In the sample, the app's I had given you earlier if you just run the project "AndroidTest" and click the home button in the logs you can see that the 2nd Activity is put on Pause, and when you launch it again from the "Recent App's" list the 2nd Activity is Destroyed.

In a scenario where the Activity's above the Single Instance activities (ActivityB) are not removed from the Back Stack, and another application request this Activity (ActivityB) it may not be shown and the intent may be dropped. But this has extremely fewer chances of happening because the user will have to press the Home button and but the current Task\App in the BackStack before he could navigate to another Task\App.

Hence the warning

The other modes — singleTask and singleInstance — are not appropriate for most applications, since they result in an interaction model that is likely to be unfamiliar to users and is very different from most other applications.

I hope this solves your doubts.

Rahul
  • 3,293
  • 2
  • 31
  • 43
Errol Dsilva
  • 177
  • 11
  • thanks, but the answer in this post makes me confuse: http://stackoverflow.com/questions/3219726/android-singletask-or-singleinstance-launch-mode there's never more than one instance of a "singleTask" or "singleInstance" activity, so that instance is expected to handle all new intents... However, a "singleTask" activity may or may not have other activities above it in the stack. If it does, it is not in position to handle the intent, and the intent is dropped. (Even though the intent is dropped, its arrival would have caused the task to come to the foreground, where it would remain.) – cn1h Jul 26 '12 at 12:27
  • 1
    "Which means that when you click the home button all the activites above the single task activity (which in your case is ActivityB) are removed from the stack." Just found this in a project, this is the case. Thanks. – Lou Morda Jan 17 '13 at 05:28
  • The "Sendspace" link is broken – Salim Mahboubi Aug 18 '15 at 18:03
  • "The other modes — singleTask..."? "singleTask" is another mode than "singleTask"? Something must be wrong here... – The incredible Jan Feb 28 '18 at 13:02
  • I have just created an app demo to test the statement "Which means that when you click the home button all the activites above the single task activity (which in your case is ActivityB) are removed from the stack." But activities above single-task activity remains in the memomy contrary to the answer – Varun Ajay Gupta May 20 '20 at 04:05
2

Correct, whenever singleTask activity is launched it comes foreground clearing all activities currently present above it, if your singleTask activity is on the top, it will behave same as singleTop.

P.S - onCreate is not called 2nd time, instead onNewIntent is called.

check this link, very well explained about launchmodes. https://medium.com/android-news/android-activity-launch-mode-e0df1aa72242

shubham chouhan
  • 580
  • 7
  • 8