Android Activity launch mode an instance

Source: Internet
Author: User

Reference: http://blog.csdn.net/ljz2009y/article/details/26621815

An app three pages the second page is set to Singltask

<?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:icon=" @drawable/ic_ Launcher "android:label=" @string/app_name "> <activity android:label=" @string/app_name "Android:na Me= "Com.jg.zhang.androidtasktest.MainActivity" > <intent-filter> <action android:name            = "Android.intent.action.MAIN"/> <category android:name= "Android.intent.category.LAUNCHER"/> </intent-filter> </activity> <!--android:taskaffinity= "Com.jg.zhang.androidtas Ktest.second "android:alwaysretaintaskstate=" true "android:allowbackup=" true "-<ac Tivity android:name= "Com.jg.zhang.androidtasktEst. Secondactivity "android:launchmode=" Singletask "> <intent-filter > <actio n android:name= "com.jg.zhang.androidtasktest.SecondActivity"/> <category android:name= "android.intent . category. DEFAULT "/> </intent-filter> </activity> <activity android:name=" COM.JG . zhang.androidtasktest.ThirdActivity "android:label=" @string/app_name "> </activity> </ap Plication> </manifest>

  

Execute the following command on the command line adb shell Dumpsys activity with the following output:
Taskrecord{412ded08 #8 A Com.jg.zhang.androidtasktest}
Run #2: Activityrecord{412c91e8 com.jg.zhang.androidtasktest/. Secondactivity}
Run #1: activityrecord{412c08a0 com.jg.zhang.androidtasktest/. Mainactivity}

and click the button in the Mainactivity interface to turn on secondactivity. In this example, the startup mode of Secondactivity is Singletask. According to the official documentation, Secondactivity will be opened in a new mission. But look at the printed log and find the same ID for the task where mainactivity and Secondactivity are located.

So, unlike official documentation, Mainactivity and Secondactivity are launched in the same task. In fact, setting the startup mode to Singletask,framework will only be marked as being launched in a new task when the activity is started, and is subject to other conditions if it is started in a new task . Now add a taskaffinity property in Secondactivity

<activity android:name= "com.jg.zhang.androidtasktest.SecondActivity"           android: Taskaffinity= "Com.jg.zhang.androidtasktest.second">     <intent-filter >         <action android: Name= "com.jg.zhang.androidtasktest.SecondActivity"/>         <category android:name= " Android.intent.category.DEFAULT "/>     </intent-filter>  

Rerun the example

Execute the ADB shell Dumpsys activity command on the command line with the following output:

Taskrecord{411e6a88 #6 A Com.jg.zhang.androidtasktest.second}
Run #3: activityrecord{411c8ea0 com.jg.zhang.androidtasktest/. Thirdactivity}
Run #2: activityrecord{412bc870 com.jg.zhang.androidtasktest/. Secondactivity}
Taskrecord{412ece18 #5 A Com.jg.zhang.androidtasktest}
Run #1: activityrecord{412924c0 com.jg.zhang.androidtasktest/. Mainactivity}

Thus, mainactivity and secondactivity run in different tasks, and the thirdactivity and secondactivity that are secondactivity initiated are running in the same task.

Taskaffinity can affect which task it is launched into when the ACTIVITY is started with the FLAG_ACTIVITY_NEW_TASK flag. This means that when the newly initiated ACTIVITY (secondactivity) is started with the FLAG_ACTIVITY_NEW_TASK flag (it can be thought that Flag_activity_new_task and singletask function the same, When the startup mode is Singletask, the framework sets its boot flag to flag_activity_new_task), The framework retrieves whether a task affinity to Com.jg.zhang.androidtasktest.second is already present (that is, a Taskrecord object)

    • If such a task exists, check if a secondactivity instance is already in the task,
      • If an instance of secondactivity already exists, the secondactivity instance in this task and task is reused, the task is transferred to the foreground, and all activity above the secondactivity is cleared. Show Secondactivity, and call Secondactivity's onnewintent ();
      • If there is no instance of Secondactivity, an instance of secondactivity 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 secondactivity is launched into this new task Com.jg.zhang.androidtasktest.second

The above discussion is the case of setting the Taskaffinity property, if Secondactivity only sets the startup mode to Singletask, not the taskaffinity, i.e. three activity taskaffinity the same, is the package name of the application, then secondactivity does not open a new task, and the decision process in the framework is as follows:

    1. When Mainactivity starts secondactivity, it discovers that the boot mode is Singletask, then set his boot flag to Flag_activity_new_task
    2. Then get the secondactivity 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 Secondactivity 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 start-up process when the Secondactivity startup mode is set to Singletask and Taskaffinity is set to Com.jg.zhang.androidtasktest.second

    1. When Mainactivity starts secondactivity, it discovers that the boot mode is Singletask, then set his boot flag to Flag_activity_new_task
    2. Then get Secondactivity's taskaffinity, namely 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 secondactivity into this new task

Android Activity launch mode an instance

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.