Android Launchmode understanding and application scenarios

Source: Internet
Author: User

When we write applications, we often involve jumps between multiple activity components. For example, a page of information, click on the next page to jump to the same pages, only the page data is not the same. In general, I will not pay attention to launchmode this property, will only use the default, this will produce a lot of repetitive activity. That's because I didn't know it before, so I'm studying it.

1. How to specify Launchmode

Basically we can specify a Launchmode property directly in the Androidmanifest.xml file

<activity    android:name=".views.MainActivity"    android:screenOrientation="portrait"    android:launchMode="singleTop"/>

There are 4 types of Launchmode

2.standard Mode Introduction

This is the default mode, and the activity instance is created each time the activity is activated and placed in the task stack.

Activity settings for this mode of behavior are a new activity, each intent to send, will always be created separately from the work. Imagine that if there is a 10 intention to write an email, there should be 10 activities carried out for each individual intention. Therefore, it is possible that an unlimited number of such activities are initiated on a device.

2.1 Before 5.0 version

This activity will be created and placed at the top of the stack, in the same task, sending a intent.

The following picture shows what happens when we share an image with a standard activity. It will be stacked on the same task, although they are from different applications.

This is what you will see in the task Manager. (Maybe a little strange)

If we change to another application and then switch back to gallery, we will still see the top gallery task Launchmode standard place. Therefore, if we need to do anything related to gallery, we must complete the current activity or close the current activity before we can return to the original place.

2.2 After 5.0 version

If all of these activities come from the same application, it can stack tasks like before 5.0

However, in some cases, the intent we send are from different intent, the new task will be created and the newly created activity will be placed in the root activity below.

Here is what we see in the heavy duty manager and before 5.0 is different

This is because the improved lollipop of the task management system makes it more meaningful. Lollipop, you can switch back to the gallery because they are different tasks. You can launch another intention that a new task will be created to serve the same intent as the previous one.

2.3 Application Scenarios Example

An example of this activity is a status posting activity that consists of an email activity or social network. If you consider an activity that can work alone, serve for a single purpose.

Example references:

AlarmClock uses standard. The user can launch multiple instances of this activity and these instances can is part of the all task and anywhere in the AC Tivity stack. As a fairly simple application it doesn ' t really demand tight control of its activity

Use standard for alarms. Users can launch multiple instances of this activity, which can be part of any task or be part of any place in the active stack. As a fairly simple application, it does not really need the strict control of its activity

3.singleTop Mode Introduction

Singletop mode. It functions almost the same as standard. The only difference is that if there is already an instance of the same type of activity at the top of the stack in the other party's task, there will be no new activity creation, but is sent to an existing activity instance through the Onnewintent () method Intent, The instance is reused to invoke the Onnewintent () method of the current activity.

In Singletop mode, for the new intent, you have to be responsible for OnCreate () and Onnewintent () to control making it applicable to all situations.

3.1 Application Scenarios Example

An example use case for this pattern is a search function. We want to create a search box that will take you to a searchactivity to see search results. For a better user experience, we usually always put a search box on the search results page and let the user do another search without pressing back.

Now imagine that if we always launch new searchactivity,10 searches that serve new search results will generate 10 new activity. It will be very strange when you press back, because you have to be 10 times through these search results activity to get back to your root activity. On the other hand, if we searchactivity at the top of the stack, we'd better send a intent instance of the activity to update the search results. Now there will only be one searchactivity on the top of the stack, and you can simply press the button and return back to the previous activity. There is more meaning now.

Anyway singletop function the same task stack. If you expect a intent to be sent to an existing activity at the top of any other task, I must let you down and say it does not work. If intent is sent from another application to singletop activity, the new activity will be rolled out in the same way as standard Launchmode.

Note: Before 5.0: Put on each other's tasks, 5.0 and After (Lollipop): A new task is created.

Example references:

Browserbookmarkspage uses Singletop. While there can multiple instances of this activity, if there are already one at the top of the task ' s activity stack it would be reused and onnewintent () would be called. This-the-to-hit-once-to-return-to-browser if the bookmarks activity is started multiple times.

Browse the Bookmarks page using Singletop. While there may be multiple instances of this activity, if there is already an activity at the top of the task stack will be reused and onnewintent () will be called. This way, you only need to return to the browser once, if the bookmark activity is started multiple times.

4.singleTask Mode Introduction

This model is completely different from standard singletop. Activity using Singletask Launchmode is allowed to have only one instance (aka Singleton) in the system. If there is an active instance in the system, the entire task will be moved to the top and intent will be delivered through the Onnewintent () method. Otherwise, the new activity will be created and placed in the appropriate task.

4.1 In the same application

If there are no singletask active instances in the system, the new one will be built simply on top of the stack at the same task.

However, if there is an existing Singletask activity instance placed on it, it will automatically destroy (in the proper way the life cycle triggers) other activity, so that the instance appears on top of the stack. At the same time, a intent is sent to singletask activity through the Onnewintent () method.

There is not a good feeling in the user experience, but it is designed like this ...

The system creates a new task and instantiates the activity at the root of the new task.
Note: The system creates a new task and instantiates the root of the active new task.

But from the experiment, it doesn't seem to work. A single-task activity is still the active stack of tasks on top of the stack. From which we can see what the Dumpsys activity command displays.

If you want an activity like a description of the Singletask document: Create a new task and take the activity as an activity. You need to specify an activity such as the Taskaffinity property for Singletask.

The following is an example of the result of starting singletaskactivity

Consider whether to use Taskaffinity's behavior, which is your job.

4.2 Partnering with another application

Once a Intent is sent from another application and there are no active instances created in the system, the new task creates a newly created activity as a root activity.

Unless there is an application that is a task that has the presence of calling Singletask activity, the creation of a new activity will be placed on top.

If there are any active instances where the task exists, the entire task will be moved to the top and each individual activity on the Singletask activity will be destroyed according to the normal life cycle. If you press the Back button, the user must go through the activity in the stack before returning to the caller task. That is, the activity in the task#1 in the diagram is closed first, and then back to task#2.

4.3 Scenario Examples

An example use case for this pattern is any one entry point activity such as an email client's Inbox page or social network timeline. In any case, you must use this pattern wisely, and activities in this mode may be corrupted.

Browseractivity uses Singletask. There is the only one browser activity at a time and it doesn ' t become part tasks This send it intents to open web pages. While it might return to whatever most recently launched it's when you hit back it's actually fixed at the bottom of its OW n Task activity stack. It would share it task with activities The IT launches like bookmarks

Browseractivity use Singletask. With only one browser active time, it does not become part of the task of putting it in an attempt to open the Web page. While it may return to any recently launched it, when you strike back it is actually fixed at the bottom of its own task activity stack. It will share its tasks with activities that it launches like bookmarks.

5.singleInstance Mode Introduction

This pattern is fairly close to Singletask, and a single activity instance can exist in the system. The difference is that the mission held the event and there was only one activity. If this activity invokes another activity, a new task is automatically created to place the new activity. Similarly, if the singleinstance activity is called, the new task will be created to place the activity.

In any case, the result is rather strange. From the information provided by Dumpsys, it has two tasks in the system, but only one of the most recent decisions out of the task manager, move to the top. So while there is a task that still works in the background, we can't switch to the foreground. It doesn't make any sense at all.

This is the singleinstance activity is called while an activity already exists in the task.

However, the new task is not visible in Task Manager

Since this task may have only one activity, we cannot switch back to task # 1. The only way to do this is to restart the application, but it appears that the SingleInstance task will be hidden in the background.

Simply assign the Taskaffinity property to the singleinstance activity to make the task manage multiple tasks.

<activity    android:name=".SingleInstanceActivity"    android:label="singleInstance launchMode"    android:launchMode="singleInstance"    android:taskAffinity="">

This will see the hidden task

5.1 Scenario Examples

Alarmalert uses SingleInstance. One alert activity at a time and it's always the its own task. Anything it launches (if anything) becomes part of its own new task

Alarmalert use SingleInstance. There is only one alert activity at a time and it is always its own task. Anything it starts (if any) becomes part of its own new task

This pattern is seldom used. Some of the real use cases are one for launching or application activities, and you are 100% definitely only one activity. In short, I recommend that you do not use this mode unless it is really necessary.

6.Intent Flags

Launchmode is the behavioral pattern that prescribes your own activity initiation, and intent.flag is the behavior pattern of other activities that you expect to be initiated by you.

In addition to assigning the startup mode directly to the Androidmanifest.xml, we are also able to sign more behavioral allocations through the so-called intent, such as:

Intent intent = new Intent(StandardActivity.this, StandardActivity.class);intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);startActivity(intent);
7. Summary and References

7.1 Summary
[1] Standard mode
This is the default mode, and the activity instance is created each time the activity is activated and placed in the task stack.

[2] Singletop mode
If there is an instance of the activity at the top of the task, the instance is reused (Onnewintent () of the instance is invoked), otherwise a new instance is created and placed on top of the stack, even if an instance of the activity already exists in the stack, and a new instance is created as long as it is not at the top of the stack.

[3] Singletask mode
If an instance of the activity is already in the stack, the instance is reused (Onnewintent () of the instance is invoked). When reused, the instance is returned to the top of the stack, so instances above it will be moved out of the stack. If the instance does not exist in the stack, a new instance is created to be put into the stack.

[4] singleinstance mode
Create an instance of the activity in a new stack and have multiple apps share that activity instance in the stack. Once the activity instance of the pattern already exists in a stack, any application that activates the activity will reuse the instance in that stack (the onnewintent () of the instance is invoked). The effect is that multiple apps share an app, regardless of who activates the Activity into the same app.

Finally, the materials are all read, learning a lot. Recently in writing the project will use the Launchmode, will not understand the previously vague place, understand some. Ax said is very right, so first read the material study, and then through the project practiced hand deepen the impression.

7.2 References

1.Understand Android Activity ' s Launchmode:standard, Singletop, Singletask and SingleInstance

2.http://stackoverflow.com/questions/2626218/examples-for-android-launch-modes

3. Basic Summary Chapter Two: Four kinds of launchmode of activity

4. Android Launchmode (four startup modes) application scenarios and examples

5.Android activity Launchmode and Intent.flag relationship

Android Launchmode understanding and application scenarios

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.