Android Activity Launch mode an instance SingleInstance

Source: Internet
Author: User

Reference: http://blog.csdn.net/zhangjg_blog/article/details/10923643#

The characteristics of singleinstance can be attributed to the following three articles:
    1. Activity initiated in SingleInstance mode is globally unique, i.e. there will only be one instance in the system.
    2. Activity initiated in SingleInstance mode is exclusive, that is, it will occupy one task on its own, and any activity that is opened by him will be run on other tasks (as described in the official document, Activity in SingleInstance mode does not allow other activity to exist in a common task with it)
    3. Other activity that is activated by activity in the SingleInstance mode can open a new task, but it may not necessarily open a new task or open a task that already exists.
<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/ Android "package=" Com.jg.zhang.androidtasktest "android:versioncode=" 1 "android:versionname=" 1.0 "> <use        S-SDK android:minsdkversion= "ten" android:targetsdkversion= "/> <application android:allowbackup=" true " android:icon= "@drawable/ic_launcher" android:label= "Androidtasktest" > <activity android:name= "com.jg . zhang.androidtasktest.MainActivity "> <intent-filter> <action android:name=" Android. Intent.action.MAIN "/> <category android:name=" Android.intent.category.LAUNCHER "/> &lt ;/intent-filter> </activity> <activity android:name= "Com.jg.zhang.androidtasktest.Secon Dactivity "android:launchmode=" singleinstance "> <intent-filter> <actio n android:name= "Com.jg.zhang.androidtasKtest. Action_my "/> <category android:name=" Android.intent.category.DEFAULT "/> </intent-f ilter> </activity> <activity android:name= "Com.jg.zhang.androidtasktest.ThirdActivity" /> </application> </manifest>

  

As can be known from the above manifest file, the application includes three activity, mianactivity,secondactivity,thirdactivity, respectively, where secondactivity startup mode is set to SingleInstance. Mianactivity can turn on the secondactivity,secondactivity to turn on the thirdactivity. And in order to enable secondactivity in other applications, a intentfilter is set for secondactivity so that the secondactivity can be turned on in other applications using implicit intent.

In order to better verify the global uniqueness of singleinstance, another application is needed to make some modifications to the above AndroidTaskTest1. AndroidTaskTest1 only needs a mianactivity, clicking the button in Mainactivity will open secondactivity in the Androidtasktest app. The code for opening the secondactivity in the Androidtasktest app is as follows

/** * This method is specified in the Android:onclick property of the button in the layout * android:onclick= "launchotheractivity" * @param v */public void Launchotheractivity (View v) {Intent Intent = new Intent ();//The following ACTION is "Com.jg.zhang.androidtasktest.ACTION_MY"// The Actionintent.setaction ("Com.jg.zhang.androidtasktest.ACTION_MY") of secondactivity in Androidtasktest application; StartActivity (intent);}

Do the following: Install the Androidtasktest application, click the Mainactivity button, open secondactivity, you can see the following log output:

Executes the ADB shell Dumpsys activity command with the following output:
Taskrecord{411189e0 #9 A Com.jg.zhang.androidtasktest}
Run #2: Activityrecord{4129af80 com.jg.zhang.androidtasktest/. Secondactivity}
taskrecord{41305528 #8 A Com.jg.zhang.androidtasktest}
Run #1: Activityrecord{41296e60 com.jg.zhang.androidtasktest/. Mainactivity}
This shows that the activity of the SingleInstance mode will always run in a new task (provided there is no such instance in the system).

To verify its global uniqueness, do the following: Install another app AndroidTaskTest1, click the button in the mainactivity to open the secondactivity in the Androidtasktest app. See print out a new log:

Executes the ADB shell Dumpsys activity command with the following output:

Taskrecord{411189e0 #9 A Com.jg.zhang.androidtasktest}

Run #3: Activityrecord{4129af80 com.jg.zhang.androidtasktest/. Secondactivity}

taskrecord{412dc788 #12 A Com.jg.zhang.androidtasktest1}

Run #2: activityrecord{4121c628 com.jg.zhang.androidtasktest1/. Mainactivity}

taskrecord{41305528 #8 A Com.jg.zhang.androidtasktest}

Run #1: Activityrecord{41296e60 com.jg.zhang.androidtasktest/. Mainactivity}

It is known from the red font that the secondactivity opened is the last secondactivity created with the number 4129af80, and the log does not output information about secondactivity again. Description Secondactivity is not re-created. It can be concluded that the activity initiated in SingleInstance mode is a singleton in the whole system, and if an instance is already present when the activity is started, it will be dispatched to the foreground and reused by this instance.

Here's a second feature: The activity started in SingleInstance mode is exclusive, that is, it will occupy a single task, and any activity that he opens will run in other tasks to reinstall the Androidtasktest app. Click the button in the mainactivity, turn on secondactivity, click the button in Secondactivity, turn on thirdactivity.  You can see the following log output: ... Secondactivity is in the task of 16, secondactivity initiated by the thirdactivity task is 15, which means activity initiated in singleinstance mode is exclusive, that is, it will occupy one task on its own, and any activity that is opened by him will run in other tasksThe next step is to verify the third feature: Other activity that is activated by the activity of the singleinstance mode, can be started in a new task, but not necessarily open a new task, may also open in an existing task to face the second characteristic of the verification can be seen, The thirdactivity started by Secondactivity is not running in a newly opened task, but is running in the same existing task as mainactivity, so under what circumstances will thirdactivity start a new task? Now make the following modifications to the program's manifest file, adding an attribute taskaffinity for thirdactivity:

         <activity android:name= "com.jg.zhang.androidtasktest.ThirdActivity"             android:taskaffinity= " Com.jg.zhang.androidtasktest.second "/>

  

Reinstall the Androidtasktest app, perform the same action as in the previous step: Click the button in the mainactivity, turn on secondactivity, click the button in the Secondactivity, and turn on thirdactivity. You can see the following output:

Executes the ADB shell Dumpsys activity command with the following output:

Taskrecord{413551b0 #20 A Com.jg.zhang.androidtasktest.second}
Run #3: activityrecord{412de9c0 com.jg.zhang.androidtasktest/. Thirdactivity}
taskrecord{4134b268 #19 A Com.jg.zhang.androidtasktest}
Run #2: activityrecord{412a36a0 com.jg.zhang.androidtasktest/. Secondactivity}
Taskrecord{413131e8 #18 A Com.jg.zhang.androidtasktest}
Run #1: Activityrecord{41271e10 com.jg.zhang.androidtasktest/. Mainactivity}

As can be seen, the thirdactivity initiated by Secondactivity started in a new task, that is, a new task was created when the thirdactivity was started. This means that activity A in the SingleInstance mode can open a new task when it opens another activity B, but is not really a new task, but also subject to other conditions, this condition is: the current system is not already have an activity The Taskaffinity property of B specifies the task. In fact, this behavior is the same as when the Singletask starts. When the activity's startup mode is set to Singletask, the system will add the FLAG_ACTIVITY_NEW_TASK flag to it at startup, and activity activated by SingleInstance mode activity, The system will also add the FLAG_ACTIVITY_NEW_TASK flag to it at startup, so the situation is the same when they are started, the above is verified singletask, and now it is explained again:

Since thirdactivity is initiated by the activation mode for singleinstance type ACTIVITY (that is, secondactivity), the framework adds the FLAG_ACTIVITY_NEW_TASK flag to it, when The framework retrieves a task that already has a affinity of com.jg.zhang.androidtasktest.second (that is, the thirdactivity taskaffinity attribute).

    • If such a task exists, check if a thirdactivity instance is already in the task,
    1. If an instance of thirdactivity already exists, the thirdactivity instance in this task and task is reused, the task is transferred to the foreground, and all activity above thirdactivity is cleared, showing thirdactivity , and call Thirdactivity's Onnewintent ().
    2. If there is no instance of Thirdactivity, an instance of thirdactivity is created in this task, and the OnCreate () method is called
    • If such a task does not exist, a new affinity task is created, and the thirdactivity is launched into this new task Com.jg.zhang.androidtasktest.second

If Thirdactivity does not set taskaffinity, that is, thirdactivity and mainactivity taskaffinity are the same, the package name is applied, then thirdactivity will not open a new task, The decision process in the framework is as follows:

    1. When the secondactivity starts thirdactivity, because Secondactivity is singleinstance, set the thirdactivity boot flag to Flag_activity_new_task
    2. Then get the thirdactivity taskaffinity, which is the package name Com.jg.zhang.androidtasktest
    3. Check to see if there is a task affinity for com.jg.zhang.androidtasktest, this task exists, is the task that mainactivity is in, this task is opened when the mainactivity is started
    4. Now that this task exists, retrieve an instance of Thirdactivity in this task and discover that there is no
    5. Start an instance of secondactivity in this existing task

To make a clear comparison, list the startup process when the Taskaffinity property of Thirdactivity is set to Com.jg.zhang.androidtasktest.second

    1. When the secondactivity starts thirdactivity, because Secondactivity is singleinstance, set the thirdactivity boot flag to Flag_activity_new_task
    2. Then get Thirdactivity's taskaffinity, which is Com.jg.zhang.androidtasktest.second
    3. Check if there is already a affinity for Com.jg.zhang.androidtasktest.second task, this task is not present
    4. Create a new affinity task for Com.jg.zhang.androidtasktest.second, and launch thirdactivity to this new task
The singleinstance has also been introduced.

Android Activity Launch mode an instance SingleInstance

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.