Activity is one of the most brilliant concept on Android from its well-design architecture on memory management which lets Multitasking works perfectly on this most popular mobile operating system.

Anyway, Activity is not just to be launched on the screen. The way it is launched is also concerned. There are so many details in this topic. One of those that is really important is launchMode, which is the one that we are going to talk about in this blog.

Since each Activity is made to work in different purpose. Some is designed to work separately with each Intent sent for example an Activity for email composing in email client. While some is designed to work as a singleton for example an email’s inbox Activity.

That’s why it does matter to specify whether Activity is needed to be created a new one or to use the existed one, or it may leads to the bad UX or malfunctional. Thanks to Android’s core engineer. It is the way easy to make it done with some help of launchMode which is designed for this especially.

Assign a launchMode
Basically we could assign a launchMode directly as an attribute of tag inside AndroidManifest.xml file list this:
<activity
android:name=”.SingleTaskActivity”
android:label=”singleTask launchMode”
android:launchMode=”singleTask”>
There are 4 types of launchMode available. Let’s see it one by one.

standard
This is the default mode.

The behavior of Activity set to this mode is a new Activity will always be created to work separately with each Intent sent. Imagine, if there are 10 Intents sent to compose an email, there should be 10 Activities launch to serve each Intent separately. As a result, there could be an unlimited number of this kind of Activity launched in a device.

Behavior on Android pre-Lollipop

 

standard –pre-lollipop
standard — pre-lollipop
standard — pre-lollipop taskmanager

If we switch the application to the another one and then switch back to Gallery, we will still see that standard launchMode place on top of Gallery’s task. As a result, if we need to do anything with Gallery, we have to finish our job in that additional Activity first.

Behavior on Android Lollipop

If those Activities are from the same application, it will work just like on pre-Lollipop, stacked on top of the task.

standard — lollipop — from same task

But in case that an Intent is sent from a different application. New task will be created and the newly created Activity will be placed as a root Activity like below.

standard — lollipop — from different application
standard — lollipop taskmanager

This happens because Task Management system is modified in Lollipop to make it better and more make sense. In Lollipop, you can just switch back to Gallery since they are in the different Task. You can fire another Intent, a new Task will be created to serve an Intent as same as the previous one.

 

singleTop

The next mode is singleTop. It acts almost the same as standard one which means that singleTop Activity instance could be created as many as we want. Only difference is if there already is an Activity instance with the same type at the top of stack in the caller Task, there would not be any new Activity created, instead an Intent will be sent to an existed Activity instance through onNewIntent() method.

singleTop

Anyway singleTop works with the same task as caller only. If you expect an Intent to be sent to an existed Activity placed on top of any other Task, I have to disappoint you by saying that it doesn’t work that way. In case Intent is sent from another application to an singleTop Activity, a new Activity would be launched in the same aspect as standard launchMode (pre-Lollipop: placed on top of the caller Task, Lollipop: a new Task would be created).

 

singleTask

This mode is quite different from standard and singleTop. An Activity with singleTask launchMode is allowed to have only one instance in the system (a.k.a. Singleton). If there is an existed Activity instance in the system, the whole Task hold the instance would be moved to top while Intent would be delivered through onNewIntent() method. Otherwise, new Activity would be created and placed in the proper Task.

Working in the same application

If there is no that singleTask Activity instance existed in the system yet, new one would be created and simply placed on top of stack in the same Task.

singleTask from same application

But if there is an existed one, all of Activities placed above that singleTask Activity would be automatically and cruelly destroyed in the proper way (lifecycle trigged) to make that an Activity we want to appear on top of stack. In the mean time, an Intent would be sent to the singleTask Activity through the lovely onNewIntent() method.

singTask from different application

refer from: http://inthecheesefactory.com/blog/understand-android-activity-launchmode/en

Android Activity’s launchMode: standard, singleTop, singleTask and singleInstance (include pre-Lollipop)

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据