Android task and rollback stack-default startup mode

Source: Internet
Author: User

The startup mode allows you to define how a new activity instance is associated with the current task. The following two methods can define different startup modes:

1. Use the inventory file

When declaring an activity in the list file, you can specify how the activity should be associated with the task at startup.

2. Use the intent identifier

When you call the startactivity () method, you can include an identifier in the intent to declare how the new activity should be associated with the current task.

Therefore, if activity a starts Activity B, Activity B can define how it is associated with the current task in its list file, in addition, Activity A can also request how Activity B is associated with the current task. If both activities define how Activity B is associated with the current task, the request of Activity A (defined in intent) the priority of a request is higher than that of Activity B (defined in the list file ).

Note:Some effective startup modes in the inventory file are invalid for the intent identifier, and some effective startup modes for the intent identifier cannot be defined in the inventory file.

Use the inventory file

When declaring an activity in the list file, you can use the launchmode attribute of the <activity> element to specify how the activity is associated with a task. The launchmode attribute specifies an instruction about how the activity is loaded into a task. There are four different startup modes that can match this attribute:

"Standard" (default mode)

By default, the system creates a new activity instance where intent is enabled and passed in the task. This activity can be instantiated multiple times. Each instance belongs to a different task, and a task can have multiple instances.

"Singtop"

If the instance of this activity already exists at the top of the current task, the system will pass this intent to this instance by calling the onnewintent () method, instead of creating a new instance. This activity can be instantiated multiple times. Each instance belongs to a different task, in addition, a task can have multiple instances (only when the activity at the top of the rollback stack is not an existing activity instance ).

For example, assume that the rollback stack of a task is composed of Activity A, Activity B, Activity C, and activity D on the top. An intent access to a D-type activity, if D has the default "standard" Startup Mode, then this class will have a new instance started, and the stack becomes a combination of A-B-C-D-D. However, if the Startup Mode of D is "singletop", the existing D instance will receive intent passed through the onnewintent () method because it is at the top of the stack, the stack still maintains a combination of A-B-C-D. However, if the intent accesses Activity B, a new instance of Activity B will be added to the stack even if its startup mode is "singletop.

Note:When a new activity instance is created, you can press the button to return to the previous activity. However, when a new intent is processed by an existing activity instance, you cannot press the rollback button to return the status of the activity before receiving the new intent access.

"Singletask"

The system creates a new task and instantiates the activity at the root of the new task. However, if an instance of this activity already exists in another task, the system will pass the intent to the existing instance through the onnewintent () method, instead of creating a new instance. The instance of this activity is smart once.

Note:Although this activity is started in a new task, it is still returned to the previous activity when the return button is pressed.

"Singleinstance"

The mode is the same as that of "singletask", except that the system does not start any activity in the task that owns this instance. This activity is always independent and the only member of its task. Any activity started through this activity is opened in an independent task.

For example, the android browser application specifies the singletask Startup Mode in the <activity> element to declare that the browser activity should always be opened in its own task. This means that if your application releases an intent that opens the android browser, the activity of this browser will not be placed in the same task as your application. On the contrary, A new browser task is started. If the browser is already running in the background, the task is taken to the foreground to process the new intent.

Whether you start an activity in a new task or in the same task as the activity that starts it, you will always be taken to the previous activity after you press the rollback button. However, if you start an activity with the singletask startup mode specified and the activity instance is in a background task, the whole task will be taken to the foreground. At this point in time, at the top of the stack, the rollback stack contains all the activities extracted from the task. Figure 4 shows the scenario type

Figure 4. shows how an activity with the "singletask" Startup Mode is added to the rollback stack. If this activity is part of an existing background task that has its own rollback stack, the entire rollback stack will also be taken to the front and placed on the top of the current task stack.

For more information about how to use the Startup Mode in the list file, see the launchmode attribute and attribute values in the <activity> element document.

Note:The attributes specified for the activity by the launchmode attribute can be overwritten by the identifier contained in the intent of the activity to be started.

 

Use intent ID

When starting an activity, you can modify the default startup mode of the activity by adding an identifier to the intent passed to the startactivity () method. Use the following identifier to modify the default activity behavior:

Flag_activity_new_task:

Start an activity in a new task. If the activity being started is a running task, the status of the last saved task is extracted to the foreground, and accept the new intent in the onnewintent () method.

This process has the same behavior as when the launchmode attribute value is equal to singletask.

Flag_activity_single_top:

If the activity being started is the current activity (at the top of the rollback stack), the existing instance will receive a call to the onnewintent () method, instead of creating a new activity instance.

This process has the same behavior as when the launchmode attribute value is equal to singletop.

Flag_activity_clear_top:

If the started activity is already running in the current task, a new instance of the activity will not be started, but all other activities on the activit In the rollback stack will be destroyed, the onnewintent () method is used to pass the intent to the instance of the activity to be restored (it is currently at the top of the stack ).

No response value for the launchmode attribute corresponds to this behavior process.

Flag_activity_clear_top is often used in combination with the flag_activity_new_task tag. These tags are used together to locate an existing activity in another task, this activity will be placed in a position that can respond to intent.

 

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.