Learn more about activity Launchmode in Android

Source: Internet
Author: User

The activity in the Android system can be described as a great design, which is well designed in memory management, making multitasking management easy to run on Android systems. But activity is not starting to display on the screen, and its start-up method is also very learned, this article describes the activity of the startup mode of the details, correct some of the development may be wrong point of view, to help you understand the activity.

Before the wording

Before the formal wording, introduce some of the concepts mentioned in the article

    • The article later mentions the task, where the task refers to a collection of activity instances that interact with the user.
    • The activity instance in the task is stored in the form of a stack, which is the fallback stack of the activity.

This picture is more, when looking at the picture, please observe the title of the activity top, to distinguish the specific activity.

Why is there a startup mode

Each activity in the app is handled differently. In the case of a mail client, Inboxactivity is intended to show the Inbox, which is not recommended to create multiple instances. While composemailactivity is used to compose messages, you can instantiate multiple objects of this activity. Reasonable design of whether the activity object uses the existing instance or multiple creation, will make the interaction design more good, also can avoid many problems. If you want to reach the previous goal, you need to use today's activity startup mode.

How to use

It is easy to use, just add the Android:launchmode attribute to the corresponding activity element in manifest. As the following code

1 2 3 4 5
<activity    android:name=".SingleTaskActivity"    android:label="singleTask launchMode"    android:launchMode="singleTask"></activity>

The next step is to introduce Launchmode's four-value moment.

Standard

This is the default value for Launchmode, which is used by activity that does not contain android:launchmode or if the activity that is set to standard is displayed.

Once set to this value, a new activity instance is created whenever a intent request is made. For example, if you have 10 intent that compose a message, you will create 10 instances of composemailactivity to handle these intent. As a result, it is clear that this pattern creates multiple instances of an activity.

Prior to Android 5.0 performance

The newly generated instance of this activity is placed at the top of the stack that sends the intent task. To initiate activity within the same program.

The following picture shows calls between cross-programs, and the newly generated activity instance is placed at the top of the stack that sends the intent task, although they belong to different programs.

But when we open the Task Manager, it's a little bit odd that the task that should be shown is gallery, and the display interface is actually the activity of another program (because it's on top of the task's stack).

At this time if we switch from the Gallery app to the dial-up app and back to gallery, we see this non-gallery activity, and if we want to operate on gallery, we have to press the back key to return to the gallery interface. It's a little bit unreasonable indeed.

Android 5.0 and later performance

For the same application internal activity start-up and 5.0 performance, the change is that the activity between different applications start to become reasonable.

When you start an activity across apps, a new task is created, and the newly generated activity is placed in the task you just created. Such as

The task Manager also looks more reasonable when it looks at the task.

Assuming that our test program existed before, and then we shared the files from gallery to our test program, the corresponding Task Manager shows the following results.

Usage Scenario: Standard This startup mode is suitable for composing mail activity or social network message publishing activity. If you want to create an activity deal for each intent, then use standard mode.

Singletop

Singletop is virtually the same as standard, with Singletop's activity you can create many instances. The only difference is that if the target activity of the call is already at the top of the caller's task, the new instance is not created, but the current activity instance is used and the Onnewintent method of the instance is called . In Singletop This mode, we need to deal with the OnCreate and onnewintent of the activity that applies this pattern to ensure that the logic is normal.

Usage Scenarios

A typical usage scenario for singletop is the search function. Given a search box, each search query directs us to searchactivity to see the results, and for a better interactive experience, we also place such a search box at the top of the results page.

Assuming that the searchactivity boot mode is standard, then each search will create a new searchactivity instance, and 10 queries is 10 activity. When we want to return to non-searchactivity, we need to press the return key 10 times, which is obviously too unreasonable.

But if we use singletop, if Searchactivity is at the top of the stack, when a new query is made, the Searchac instance is no longer recreated, and the results are updated with the current searchactivity. When we need to return to non-searchactivity just press the return key once. The use of singletop is clearly more reasonable than before.

Summarize
    • A new target activity instance is created only if the caller and target activity are in the same task, and the target activity is at the top of the stack, using the existing target activity instance.
    • If an external program initiates singletop activity, the newly created activity before Android 5.0 will be in the caller's task, and 5.0 and later will be placed in the new task.
Singletask

Singletask This pattern is very different from the standard and Singletop mentioned earlier. activity using the Singletask startup mode will only have one instance in the system . If this instance already exists, intent will pass through onnewintent to the activity. Otherwise, the new activity instance is created.

Within the same program

If an instance of Singletask activity does not exist in the system, then it is necessary to create an instance of the activity and place the instance in the same task as the caller and at the top of the stack.

If the Singletask activity instance already exists, then all activity instances located on that activity will be destroyed in the activity fallback stack (the destruction process invokes the activity lifecycle callback). This allows the Singletask activity instance to be at the top of the stack. At the same time, intent will pass through Onnewintent to this Singletask activity instance.

However, Google's document on Singletask has such a description

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

This means that the system creates a new task and creates an activity instance at the bottom of the new task.

However, this is not the case, in my case, singletask activity and creates and places the task where the caller resides, rather than putting in a new task, which adb shell dumpsys activity can be validated.

1 2 3 4 5 6 7 8 9
 Task ID #239  Taskrecord{428efe30 #239 A=com . thecheesefactory . Lab . Launchmode U = 0 sz=2}     Intent { Act=Android.Intent.Action.MAIN Cat=[Android.Intent.category.LAUNCHER] FLG=0x10000000 CMP=com.thecheesefactory.Lab.Launchmode/.standardactivity } Hist #1: activityrecord{429a88d0 u0 com . thecheesefactory . Lab . Launchmode /. singletaskactivity t239 }     Intent { cmp=com. thecheesefactory . Lab . Launchmode /. singletaskactivity }       Processrecord{42243130 18965:com. thecheesefactory . Lab . Launchmode / u0a123 }       Hist #0: activityrecord{425 fec98  u0 com. thecheesefactory . Lab . Launchmode /. standardactivity t239 }          Intent { Act=Android.Intent.Action.MAIN Cat=[Android.Intent.category.LAUNCHER] FLG=0x10000000 CMP=com.thecheesefactory.Lab.Launchmode/.standardactivity } Processrecord{42243130 18965:com. thecheesefactory . Lab . Launchmode / u0a123 }      

However, it is not impossible to implement the description of the document, we need to set Launchmode to Singletask, plus the Taskaffinity property.

1  2  3  4  5  6  
 <activity    android:name=   " . Singletaskactivity "   android:label=  " Singletask launchmode "   android:launchmode=   "Singletask"    android:taskaffinity=   " >
   </ACTIVITY>    

To complete the above modification, we look at the effect, the change of task as

Also, the Task Manager effect in the system changes accordingly

Across applications

If an instance of Singletask activity does not exist in the system when the intent is passed across the application, create a new task and then create an instance of the Singletask activity and put it into a new task. The task changes as follows.

The task manager of the system will also change as follows

If the application process where the singletask activity exists, but the Singletask activity instance does not exist, starting the activity from another application, the new activity instance is created and placed in the task where the owning process resides. and is positioned at the top of the stack.

In a more complex case, if the Singletask activity instance exists and is started from another program, the activity's task is moved to the top, and in this task, it is located in the Singletask All activity on the activity instance will be destroyed normally. If we press the return key, we will first fall back to the other activity in this task until the current task's activity fallback stack is empty before returning to the caller's task.

In, when the albums in Task2 start sharing call Task1 in the Singletask activity, and the activity instance exists, and is located in the third position in the fallback stack in Task1 (top to bottom order), Then the two activity instances located above the activity will be destroyed so that the activity instance is on top of the stack. At this time, Task1 in the fallback stack only two activity, if clicked back, then will be back to not the album app, but singletask activity stack location under the activity, click Back again to return to the album app.

Usage Scenarios

This pattern is used in many scenarios similar to the mail client's Inbox or social app timeline activity. Both scenarios require that the corresponding activity keep only one instance, but use this pattern sparingly, as it can destroy other activity without the user's awareness.

SingleInstance

This pattern is similar to Singletask because they have only one instance in the system. The only difference is that the task that holds the SingleInstance activity instance can only hold an activity instance of that pattern. If you start another activity from the SingleInstance activity instance, the activity instance is placed in another task. Similarly, if the singleinstance activity is initiated by another activity, it is also placed in a different task than the caller.

Although it is a two task, in the system's task manager, it always shows one, which is the task at the top.

In addition, when we enter this application from the Task Manager, it is not possible to go back to Task1 by the return key.

Fortunately there is a way to solve this problem, that is mentioned earlier taskAffinity="" , for Launchmode for the singleinstance activity to add this attribute.

1  2  3  4  5  6  
 <activity    android:name=   " . Singleinstanceactivity "   android:label=   "SingleInstance launchmode"    android:launchmode=   "singleinstance"    android:taskaffinity=   >   </activity> 

Run the modified code again to see the task Manager, and the result is reasonable.

Use case

The use of this model is relatively rare and may be used in launcher. Or you're sure you need to have only one instance of the activity. It is recommended to use sparingly.

Intent Flags

In addition to setting Launchmode in the manifest file, you can also set flag in intnet to achieve the same effect. The following code allows standardactivity to be started Singletop mode.

1 2 3
Intent intent = new Intent(StandardActivity.this, StandardActivity.class);intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);startActivity(intent);

SOURCE information:

https://inthecheesefactory.com/blog/understand-android-activity-launchmode/en


Learn more about activity Launchmode in Android

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.