Warm tips from Android Launchmode

Source: Internet
Author: User

Sissy said

As an Android small white, or willing to share my exploration journey with you, then let us first a little from Android Launchmode warm tips ~ (≧▽≦)/~.

Background knowledge

“现在我的手中有一摞牌,但是我让你看到的只有一张。”

“其他牌去哪了呢?”

“其他张牌都被最上面这张牌挡住了呀。”

The above paragraph is a very vivid explanation of the relationship between the acitivity and the task stack.

Next, let's look at the task--task first.

Task
    • Task is a collection of activities, which is managed by the back stack, and relies on the advanced post-out queue.
    • There is at least one activity in each task, and the activity of the new instance is placed on top of the stack
    • Task can be switched to the background
Taskaffinity
    • Taskaffinity This attribute primarily determines which task each activity belongs to.
    • By default, activity in the same package shares the same affinity (task sharing).
Launchmode
    • Used to set how the activity should start
    • There are four main modes:
      • Standard (default)
      • Singletop
      • Singletask
      • SingleInstance
Is there a situation where you need to change the default settings pinch?
    • There are multiple portals pointing to the app
      • Launcher
      • Notification
      • Share Intent
    • "Single-Case" Activity
      • For example, a browser wants to share data with many instances.
      • What can be changed?
      • Launchmode property of activity
      • Flag of Intent

In these cases, our default boot mode is not sufficient.

So here's the problem. Boot mode which strong ([email protected][email protected]=)?
Next, let's get closer to Android's 4 startup modes ~

Standard
    • Activity in this loading mode can have multiple instances;
    • Its instances can reside in different tasks;
    • A task can have multiple instances of it

Come on, let's do an experiment together.

    • Experiment Configuration:
<?xml version= "1.0" encoding= "Utf-8"?><manifest xmlns:android="Http://schemas.android.com/apk/res/android"  package ="Com.xixi.standardlaunchmode" >        <applicationandroid:allowbackup="true"android:icon="@ Mipmap/ic_launcher "android:label=" @string/app_name "android:theme=" @style/apptheme " >                                        <activityandroid:name=". Activitya "android:label=" @string/app_name " />                                <activityandroid:name=". Activityb "android:taskaffinity=" Com.other " />                             </Application></manifest>
    • Experimental process

    • Experiment Analysis:

      • How do you draw the pinch of this picture?
      • First, we can check the log first.
      • Next, we'll use "adb shell Dumpsys" to detect the Android activity task stack.
        ??
        We can see that the standard mode of the two activity is constantly instantiated at each start, but we know Activityb taskaffinity is "Com.other" ah, why does not work pinch? ~
      • Because we did not add parameters for intent: Flag_activity_new_task, plus will not work in the future pinch?

        What kind of a diary is the pinch?
    • Performance Summary:
      • Before Lollipop, the activity is created for each intent, and the new activity is placed in the same task stack that emits the intent activity (we do not consider the taskaffinity difference);
      • In the lollipop version, the activity from the same application, with the same effect as before, was created to be placed on top of the stack, but if it was a different application activity, The activity that is created is placed in a new stack (see also: http://inthecheesefactory.com/blog/understand-android-activity-launchmode/en)
Singletop
    • In essence, this pattern is not much different from standard mode, except in a special case.
    • When an instance of the schema activity already exists on the stack top of the back stack, it is not re-instantiated, but instead invokes the onnewintent () instance
      Come on, let's do an experiment together and demonstrate the following:
    • Experiment Configuration:
<?xml version= "1.0" encoding= "Utf-8"?><manifest xmlns:android="Http://schemas.android.com/apk/res/android"  package ="Com.xixi.standardlaunchmode" >        <applicationandroid:allowbackup="true"android:icon="@ Mipmap/ic_launcher "android:label=" @string/app_name "android:theme=" @style/apptheme " >                                        <activityandroid:name=". Activitya "android:label=" @string/app_name " />                                <activityandroid:name=". Activityb "android:taskaffinity=" Com.other " />                             </Application></manifest>
    • Experimental process:
    • Experiment Analysis:

First we still look at the log:

Next, look further down the task stack:

    • Performance Summary

      • Basically the same as standard, only if the task stack already exists, you do not need to re-create, but call Onnewintent ()

      Next, we need to go into a completely different way from the first two modes, take a break:

      Av8d,r U ready? Hey, it's not enough!

Singletask

For this torture "goblin", this is described in the official documentation:

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 NE W instance. Only one instance of the activity can exist at a time.

and the truth is , is it the first experiment?

Experiment 1: The start mode of a is set to Singletask 3 activity is in the same task
    • Experiment Configuration:
<activity  android:name  = ". Activitya " android:launchmode  =" SingleTask " />  <activity  android:name  =/>  <activity  android:name  =". Activityc " android:taskaffinity  =" com.others "/>   
    • Experimental process Recording

      We can see that when you start a again, because an instance of a is already present in the task stack, the other already existing activity pops out in order to refer to the top of the stack.
    • Experiment Log Task Stack changes:


      After starting a again:


      Log:

      So, what happens if B is singletask a?
Experimental 2:b start mode is set to Singletask 3 activity are in the same task
    • Experiment Configuration:
<activity  android:name  = ". Activitya "/>  <activity  android:name  =". Activityb " android:launchmode  =" SingleTask " />  <activity  android:name  = android:taskaffinity  =" com.others "/>   
    • Experimental process Recording
    • Task Stack Record

      After starting B again:

    • Summary:
      Through the above 2 experiments, we can see:

      • In the same task (equivalent to the same app) , if there is singletask activity, it will and will only be instantiated 1 times.
      • If an instance of it already exists in the stack, it is called directly through the onnewintent, and the other instances above it are destroyed by the stack;
      • By experiment 2 We can know that when an instance of the Singletask activity is not on the bottom of the stack, it is not acting as root, because it does not restart a new task, which indicates that the official document is not the same as the actual situation.
Experiment 3: A's start mode is set to Singletask 3 activity is not in the same task
    • Experiment Configuration:
      Same as experiment 1;

      • Experimental process


      After starting a again:

In this experiment, we found that Activitya was still in task 25 and was not instantiated in the new task, and when it was started again, the activity on top of it was ended in order to put it on the stack.

    • Performance Summary
      • Activity is singletask start mode, there are only 1 instances;
      • If the activity in the same task wants to start it again, the activity on top of it in the stack will be all over, so that it is at the top of the stack of the task stack;
      • If you want to make it start in a new task, you need to set the Taskaffinity property for it
SingleInstance

Same as Singletask with only one instance so what's the difference?

Experiment 1: The start mode of a is set to SingleInstance 3 activity is in a different task
    • Experiment Configuration:
<activity android:name=".ActivityA" android:launchMode="singleInstance" /><activity android:name=".ActivityB" /><activity android:name=".ActivityC" android:taskAffinity="com.others" />
    • Experimental process:
    • An analysis of experimental analysis



      After starting a again:

      So what's the log like?

    • Summary:

      • The activity of singleinstance is the root of the task, and only one of the tasks in which it resides is acitivity.
The startup mode of the experimental 2:b is set to SingleInstance 2 activity in a different task
    • Experiment Configuration:
<activity  android:name  = ". Activitya "/>  <activity  android:name  =". Activityb " android:launchmode  =" SingleInstance "/>  <activity  android:name  = android:taskaffinity  =" com.others "/>   
    • Experimental process

      After starting B again:

    • Performance Summary

      • Activity is singleinstance boot mode, there are only 1 instances in the system;
      • It is root Activity;
      • It is more withdrawn and is determined not to tolerate the sharing of 1 tasks with other activity;
      • In simple terms:
      task in the tasks{  if(hasSingleInstanceActivity) {    tasktrue;    task1;    true;    true;  } }
Summarize
    • Android Launchmode:
      • There are many instances of standard:activity, and different instances can be located in different tasks;
      • Singletop:activity is the same as standard, only when the top of the stack has its instance, there is no need to instantiate it;
      • Singletask:activity has only 1 instances, and other activity emits intent need to start it, and it is in the same stack of activity will be ended, so that it is located on the top of the stack;
      • SingleInstance: Very much like singletask, but it is more aloof, that is, there is only one task in the system that holds it, and this task only stores it, and it has only one.
Access information

Http://www.slideshare.net/RanNachmany/manipulating-android-tasks-and-back-stack
Http://developer.android.com/guide/components/tasks-and-back-stack.html
http://inthecheesefactory.com/blog/understand-android-activity-launchmode/en
There is also a Chinese version of the HA acitivity start mode graphics

http://blog.csdn.net/guolin_blog/article/details/41087993
http://blog.csdn.net/luoshengyang/article/details/6714543

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Warm tips from Android Launchmode

Related Article

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.