Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting launchMode="singleTask" vs setting activity launchMode="singleTop"

I have an app that is very hierarchical (activities are similar to League > Team > Position > Player) and so I've made each activity singleTop in order to keep navigation sensible and to prevent duplicate instances.

Now I'm making my second app and I've seen it suggested to declare my application to be singleTask to prevent duplicate instances. Could someone help explain the advantages of each approach?

My new app is just an activity with 3 fragments and then I'll probably add a settings activity and maybe a FAQ.

EDIT: I just realized that singleTask is NOT preventing duplicate instances of my app, as I had thought. Now looking for the right way to handle this...

like image 927
NSouth Avatar asked Sep 10 '14 19:09

NSouth


People also ask

What is the difference between singleTop and singleTask?

The modes fall into two groups. standard and singleTop comes in one side and singleTask and singleInstance comes in another side. The main difference between standard and singleTop is in standard, every time a new intent for standard activity, a new instance is created.

What is launchMode singleTop?

Launch mode 'SingleTop' — Multiple instances Conditionally: Another normal launch mode. If Activity's launch mode is defined as a “SingleTop” and Activity is already on Top of target Task. It will simply route to the same Activity by onNewIntent() method otherwise, will behave normal and create a new instance.

What is meant by singleTop in activity instance?

singleTopIf an instance is not present on top of task then new instance will be created. Using this launch mode you can create multiple instance of the same activity in the same task or in different tasks only if the same instance does not already exist at the top of stack.


2 Answers

I think your definition of singleTop and singleTask is a little off. SingleTop could produce a duplicate instance. Lets use your example, League > Team > Position > Player. If there is a button in the player screen that will take you to the league screen, it will become League > Team > Position > Player > League.

Whereas singleTask guarantees that only one instance of the activity can exist.

like image 133
CChi Avatar answered Sep 18 '22 14:09

CChi


Android activity launchMode

4 modes...

  1. "standard"
  2. "singleTop"
  3. "singleTask"
  4. "singleInstance"

The default mode is "standard".

The modes fall into two groups. standard and singleTop comes in one side and singleTask and singleInstance comes in another side.

The main difference between standard and singleTop is in standard, every time a new intent for standard activity, a new instance is created. In case of singleTop too, a new instance is created but an instance of the activity is already in top of the stack, it wont create a new instance.

Actually, the issue comes , when we download an application from a server and launch it and open it from there itself. After launching the application, press home button. Then click the all programs and select the icon of the application from home screen. Then another activity will be created in the case of standard, but in singleTop , no new instance will be created.

The "singleTask" and "singleInstance" modes also differ from each other in only one respect:

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

A "singleInstance" activity, on the other hand, permits no other activities to be part of its task. It's the only activity in the task. If it starts another activity, that activity is assigned to a different task — as if FLAG_ACTIVITY_NEW_TASK was in the intent.

http://smartandroidians.blogspot.in/2010/04/activity-launch-mode-in-android.html

like image 27
Selva Avatar answered Sep 20 '22 14:09

Selva