Android Activity --- startup mode, androidactivity ---

Source: Internet
Author: User
Tags dota

Android Activity --- startup mode, androidactivity ---
Related Basic concepts:

1. Task)

The stack of several Activity sets represents a Task. The stack not only contains the Activity of its own program, but also contains the Activity of other applications across applications. This ensures the user experience and maintains the integrity of the user operation experience.
For example, ActivityA shares an article to QQActivityB. The two activities exist in different apps, but are placed in a task by the system. After sharing, the user presses the back key to return, it can be returned to the original Activity A, which ensures the user experience. 2. view the task stack of the current system
Command Line --> adb shell dumpsys activity 3. Activity-related attributesAndroid: taskAffinity: whether the attribute is defined, which has a great impact on the singleTask discussed later. If the taskAffinity attribute value of the Activity is set, activity will be started in an independent Task with the specified value as TaskName. Otherwise, it will be in the original default Task. Android: allowTaskReparenting: Mainly used for activity migration. program A has started several activities, including Activity1 (which has this attribute ), when program B needs to start it, it can be migrated to program B. Android: alwaysRetainTaskState Root ActivityEffective (ABC, A is the bottom of the stack, C is the top of the stack, only A can be ). For example, in the background, when we open the application again, the system will determine whether there is A stack with A as the root Activity. If so, use the stack directly and display the Activity at the top of the stack. Android: clearTaskOnLaunch: used to mark whether to clear the division when the Activity is started Root directoryOther Activity android: exported: whether other applications can call the current component. Default Value: Contains intent-filter. The default value is true (). If no intent-filter is used, the default value is false. Four startup modes:  Standard: when an Activity is activated (startActivity), the Activity instance is created and placed in the task stack. singleTop: if the Activity is located at the top of the task stack, no creation is required, the instance is reused and the onNewIntent () method of the instance is called to pass the Intent object to the instance. In other cases, the Activity instance must be created. (singleTop mode, only takes effect in the current task stack and startActivityForResult starts an activity with singleTop configured. The singleTop mode will not work) About onNewIntent ():According to the document, when the Activity startup mode is singleTop or the FLAG_ACTIVITY_SINGLE_TOP flag is used to start a new Activity and the Activity stack is the Activity to be started, this method of the original Activity will be called; example: QActivity is the top-stack multiplexing mode. When Qactivity is located at the top of the stack, it is opened again. onResume () --> onPause () --> onNewIntent () --> onResume (). When Qactivity is not located at the top of the stack, onNewIntent --> onRestart --> onStart --> onResume (that is, as long as the top of the stack is not a Qactivity, a new instance is created. If the top of the stack is a Qactivity, It is reused ). SingleTask: It is important to understand the taskAffinity attribute in the stack reuse mode. Task with the same taskAffinity attribute value: If no instance exists, a new Activity instance is created. If yes, the instance will be reused in the current instance. This Activity is started and the top Acitivity is cleared by identifying CLEAR_TOP.
Task with different taskAffinity attribute values: create this Activity instance in this Task. SingleInstance: similar to singleTask. The only difference is that the Task of the singleInstance Activity instance can only store one Activity instance in this mode. For example, when a Qactivity loads Wactivity, It will be placed in other tasks, if a Task with the same affinity as Wactivity exists, Wactivity is created in the Task. If the Task does not exist, a new Task is generated and merged into the stack. Commonly used Intent Flags:  FLAG_ACTIVITY_NEW_TASK: if the activity you want to start already exists in the task, the current task will be taken to the foreground (whether or not the activity is on the foreground or not, there may be other activities under the activity ). If FLAG_ACTIVITY_NEW_MULTI_TASK is used together, a new Task will be created in any situation, which is similar to singleInstance. If FLAG_ACTIVITY_CLEAR_TASK is used together, the stack is cleared first, and the target Activity is added to the stack. The stack ID remains unchanged.
For example, if the Flag is FLAG_ACTIVITY_NEW_TASK, Qactivity (Stack is LOL) starts Wactivity (Stack is DOTA), then Eactivity (Stack is DOTA) is started, and Home is returned to the desktop. Open the application, Qactivity starts Wactivity, and Eactvity will appear on the frontend, because it is started on the entire stack (the sequence remains unchanged ).

FLAG_ACTIVITY_SINGLE_TOP: equivalent to singleTop in launchMode.

FLAG_ACTIVITY_CLEAR_TOP: similar to singleTask, but it will destroy the target Activity and all the activities above it, recreate the target Activity, and do not execute onNewIntent (). Tips:When the startup mode is specified, the priority of Flags is higher than that of launchMode. When both modes are used, Flags are valid with Intent Flag. Note:  1. When the target startup mode is not standard, do not use startActivityForResult to start the target. Unexpected results may occur.
2. When the Activity executes onDestroy but does not execute finish, the instances still exist in the stack. However, if the Activity is finished, the new instance is re-added to the stack, not in the startup mode. Reprinted from: http://www.cnblogs.com/wujiancheng/Other knowledge points about Activity: Detailed explanation of Android Activity --- Activity Lifecycle

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.