Android Activity launchMode research, androidlaunchmode

Source: Internet
Author: User

Android Activity launchMode research, androidlaunchmode

Android Activity launchMode: There are four types of Launch modes: standard, singleTop, singleTask, and singleInstance. The default mode is standard. The four startup modes are divided into two groups.The four startup modes of Activity can be divided into two groups: standard and singleTop. The activity marked by the two modes can have multiple instances (initialized multiple times ), these instances can belong to any task and can be placed anywhere in the activity stack. normally, the activity marked in these two modes will be started into the task that calls startActivity (), unless the Intent object contains the FLAG_ACTIVITY_NEW_TASK mark, A new task is started. singleTask and singleInstance are in another group. The two modes of activity can only start one task. They are always at the root of the activity stack, and the device can only hold one activity instance in one time, that is, there is only one such task. LaunchMode descriptionStandard default mode. every time a new Intent object starts a standard activity, a new instance of this activity will be created to process this intent, that is, each activity instance processes an intent. similar to the standard mode, a singleTop activity instance may be created to process a new intent. however, if the target task already has an instance of this singleTop activity and it is on the top of the stack, the existing instance will accept this new intent ( onNewIntent()Method is called), new instances are not created. in other cases, for example, if an instance is on the target task but not on the top of the stack, or is on the top of a stack but not on the target task, new instances will be created, and put it on the top of the stack. At this time, the behavior is the same as standard. the only difference between singleTasksingleTask and singleInstance is that the singleTask activity allows other activities to be in its task. the activity whose startup mode is singleTask is always at the root of its task. At the same time, other activities (whose launch mode is standard and singleTop) can be started into this task. when the system starts an activity, it finds that its launchMode is singleTask. It cannot guarantee that it will actually start a new task and check the taskAffinity attribute of the activity. if the taskAffinity attribute is not specified, the default value is taskAffinity of the application, and the name is the package name. if the task specified by taskAffinity already exists, an activity is created in the task. If the task does not exist, a new task is created. in an experiment, a standard activity is used to start a singleTask activity. If taskAffinity is not specified, their taskids are the same, indicating that they are still in the same task; after a new taskAffinity is specified for the activity of singleTask, The taskId of the activity OF singleTask is different from that of standard. This indicates that a new task is started. when a singleTask activity instance is started, if such an instance already exists in the system, the instance will be scheduled to the top of the task stack, and clear all activities above the stack in its task. singleInstance: the launch mode of a singleInstance activity. Other activities are not allowed in its tasks. It is the only activity in this task. if it starts another activity, the new activity will be allocated to a different task, as if the intent contains FLAG_ACTIVITY_NEW_TASK Flag. when the system starts an activity, if it finds that its launchMode is singleInstance, a new task will be started, because this activity will not share tasks with others. therefore, unlike singleTask, singleInstance does not need to specify taskAffinity. the activity started in singleInstance will not be placed in the same task as it. Select the activity to be started based on the taskAffinity. The activity may be enabled in other existing tasks, in short, it is not in the task of singleInstance. TaskAffinity attributesThe taskAffinity attribute specifies the task to which the activity belongs. activities with the same attribute should belong to the same task in terms of concept and belong to the same application from the user's perspective. the affinity of a task is determined by the affinity of its root activity. affinity determines two things: 1. task to which activity re-parented belongs; you can view allowTaskReparentingAttribute description. 2. when an activity is started by an Intent with FLAG_ACTIVITY_NEW_TASK flag, which task is used to hold the activity. by default, all activities in an application have the same affinity. you can set the taskAffinity attribute to group activities, or put the activities not defined in the application in the same task. that is, the cross-application and cross-Process activities can be in the same task. if you want to set the affinity of an activity that does not belong to any task, you can set this attribute to an empty string. if the taskAffinity attribute of the Activity is not set, the activity inherits this attribute of the application. for application, the default affinity name is the package name specified by the manifest element. by default, all activities in an application have the same affinity. The name is the application package name. note: When you specify taskAffinity, you must start with the application package name or omit the package name, start with a dot, and add your own name. otherwise, an error is reported during installation. Up NavigationIn the App, all non-Home screens should be provided to the user with a method that allows the user to return to the prarent screen on the application logic by clicking the Up key on the action bar. from Android 4.1, that is, API 16, you can use android:parentActivityNameAttribute to specify the logical parent of each activity. After the preceding attribute specifies the parent, you can use NavUtilsAPI to navigate Up to parent. for details, see: http://developer.android.com/training/implementing-navigation/ancestral.html here to say that when navigate up to an activity on the current stack, the specific behavior is determined by the launch mode of the parent activity. if the parent activity is singleTop (or the up intent contains FLAG_ACTIVITY_CLEAR_TOP Flag), the parent activity will be taken to the top of the stack, and its status will be saved, the Navigation intent will be onNewIntent()Method. If the launch mode of the parent activity is standard (and the up intent does not contain FLAG_ACTIVITY_CLEAR_TOPFlag), the current activity and its parent will be popped up, and the new instance of the parent activity will be created to receive the intent. References:Lab project address: http://blog.csdn.net/zhangjg_blog/article/details/10923643 Activity elements: http://developer.android.com/guide/topics/manifest/activity-element.htmlTasks and Back Stack: http://developer.android.com/guide/components/tasks-and-back-stack.htmlProviding Up Navigationhttp: // developer.android.com/training/implementing-navigation/ancestral.html

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.