Android singleTask or singleInstance launch mode? [closed]
Asked Answered
P

3

84

I have an app that has a list as its main activity and then you can click items which opens a detailed view of that item. I also have a search activity that is similar to the main activity and works as intended.

However I want this search activity to only have once instance on the stack so that users can search multiple times and clicking back would return them to the previouse view that they were on before they started searching (rather than go back to the previouse search results)

both the singleTask and singelInstance launch mode seems to do what I want so Im not sure which one I should be using for this purpose and why?

Plank answered 10/7, 2010 at 15:12 Comment(0)
B
141

From the Application Fundamentals page of the Android dev guide:

By default, all the activities in an application have an affinity for each other — that is, there's a preference for them all to belong to the same task.

A "singleInstance" activity stands alone as the only activity in its task. If it starts another activity, that activity will be launched into a different task regardless of its launch mode — as if FLAG_ACTIVITY_NEW_TASK was in the intent. In all other respects, the "singleInstance" mode is identical to "singleTask".

As noted above, there's never more than one instance of a "singleTask" or "singleInstance" activity, so that instance is expected to handle all new intents. A "singleInstance" activity is always at the top of the stack (since it is the only activity in the task), so it is always in position to handle the intent. 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.)

4 Activities in a Task

Since there is never more than one instance of the Activity with either launch mode, the back button will always take you to the existing instance of the Activity in your case.

An important difference is that "singleTask" doesn't require the creation of a new task for the new Activities being launched when something is selected. Nor will it have to remove that new task on the back button each time.

Since your Activity stack does all pertain to one user "task", and it doesn't sound like you have an intricate Intent structure where singleInstance may be beneficial to always handle them, I would suggest using the singleTask launch mode.

Here is a good blog post for more info, as well as credited for the image: Android Activities and Tasks series – An introduction to Android’s UI component model

Bamboo answered 10/7, 2010 at 18:24 Comment(4)
Thanks! looks like a good read...the thing is iv realised I actually WANT multiple instance of an activity and only when the foreground activity is a search activity and another search is performed should it use that existing activity (like how the market works) And for that the singeTop is exactly what Im after but thanks for the help anyways =) thanksPlank
Hey, thanks. This well-written post helped me in a pinch.Larose
Thanks for the details, especially this one (Even though the intent is dropped, its arrival would have caused the task to come to the foreground, where it would remain.), which instantly clears my confusion. I was just thinking about what if a singleTask Activity is not at the top of the stack while I still want to navigate to it without specifying FLAG_ACTIVITY_CLEAR_TOP .Stolzer
Must Read "androidsrc.net/android-activity-launch-mode-example"Sugarplum
R
30

In a simple way-

singleTask:

The system creates a new task and instantiates the activity at the root of the new task. However, if an instance of the activity already exists in a separate task, the system routes the intent to the existing instance through a call to its onNewIntent() method, rather than creating a new instance. Only one instance of the activity can exist at a time.

Note: Although the activity starts in a new task, the Back button still returns the user to the previous activity.

singleInstance-

Same as "singleTask", except that the system doesn't launch any other activities into the task holding the instance. The activity is always the single and only member of its task; any activities started by this one open in a separate task.

Refrain answered 20/4, 2015 at 7:23 Comment(0)
Y
4

singleTask and singleInstance activities can only begin a task. They are always at the root of the activity stack. Moreover, the device can hold only one instance of the activity at a time — only one such task.
for more android:launchMode.

Yachting answered 8/4, 2014 at 10:22 Comment(3)
the last sentence is true for singleInstance, not singleTaskPownall
@SerdarS. appreciate your comment. Adding more about singleTask - The system creates a new task and instantiates the activity at the root of the new task. However, if an instance of the activity already exists in a separate task, the system routes the intent to the existing instance through a call to its onNewIntent() method, rather than creating a new instance. Only one instance of the activity can exist at a time. for more developer.android.comYachting
Got it, then i was wrong. Sorry to bother.Pownall

© 2022 - 2024 — McMap. All rights reserved.