Loading modes of four Android activities

Source: Internet
Author: User

I saw this on someone else's blog.ArticleIf you think it is good, you can get it here for reference.

Article Source: http://mobile.51cto.com/android-266522_1.htm

ApplicationProgramEach component has a life cycle, starting from the android instance responding to intent to the instance being destroyed. During this period, they may be valid or invalid, and they may be visible or invisible to users. Next we will discuss the life cycle of the four basic components, including various states in the life cycle and transition between States. The possible result of these statuses is that the process stops them and the instance is destroyed.

 

Activity StatusGenerally, activity has the following four States:

 

Active: When an activity is on the top of the stack, it is visible, focused, and user input acceptable. Android tries to maintain its activity status as much as possible, killing other activities to ensure that the current activity has enough resources to use. When another activity is activated, it will be paused.

Pause: In many cases, your activity is visible but has no focus. In other words, it is paused. The possible cause is that a transparent or non-full screen activity is activated.

When paused, an activity is regarded as an activity, but user input is not acceptable. In special cases, Android will kill a paused activity to provide sufficient resources for the activity. When an activity changes to completely hidden, it changes to stopped.

Stop: When an activity is not visible, it is "STOPPED. This activity will still store all its status and member information in the memory. However, when memory is needed elsewhere, it is most likely to be released. When an activity is stopped, a very important step is to save the data and the current UI status. Once an activity exits or is disabled, it will become available.

Waiting for use: After an activity is killed and put in front of it, it is in the waiting state. To be used, acitivity is removed from the activity stack and needs to be restarted before display and availability.

 

Four loading modes of activity

In multi-activity development of Android, there may be many ways to jump between activities. Sometimes it is common to generate a new instance and sometimes you want to jump to an original activity instance, instead of generating a large number of repeated activities. The loading mode determines the method to start a jump to an original activity instance.

In Android, there are four activity startup modes:

Standard: standard mode. A new instance is generated when startactivity () is called.

Singletop: if an instance already exists at the top of the activity stack, a new instance is not generated, but the newinstance () method in the activity is called. If it is not at the top of the stack, a new instance is generated.

Singletask: This instance will be generated in a new task. This instance will be used for each call in the future and no new instances will be generated.

Singleinstance: this is basically the same as singletask. There is only one difference: In this mode, only the activity instance can exist in the task where the activity instance is located, but not other instances.

You can set these startup modes in the androidmanifest. xml file of the function list to set the launchmode attribute.

RelatedCodeSome labels are also available. For example, if you want to enable only one instance, you can use intent. the flag_activity_reorder_to_front flag indicates that if the activity has been started, no new activity will be generated. You just need to add the activity instance to the top of the stack.

    1. IntentIntent=NewIntent (reorderfour. This, reordertwo. Class );
    2. Intent. addflags (intent. flag_activity_reorder_to_front );
    3. Startactivity (intent );

The loading mode of an activity is subject to the interaction control between the flag set in the intent object that starts the activity and the feature value of the activity element in the manifest file.

The following are some features that affect the loading mode:

The core intent flags include:

Flag_activity_new_task
Flag_activity_clear_top
Flag_activity_reset_task_if_needed
Flag_activity_single_top
Core features include:
Taskaffinity
Launchmode
Allowtaskreparenting
Cleartaskonlaunch
Alwaysretaintaskstate
Finishontasklaunch

 

Differences between the four loading modes

Difference between tasks

Generally, the target task of the activity of "standard" and "singletop" is in the same task as the receiver of the received intent, which is equivalent to who calls it, it is in the same task.

Unless Intent includes the flag_activity_new_task parameter. If the flag_activity_new_task parameter is provided, it is started into another task.

"Singletask" and "singleinstance" always take the activity to be started as the root element of a task and they will not be started into another task.

Whether multiple instances are allowed

"Standard" and "singletop" can be instantiated multiple times and can exist in different tasks. In such instantiation, a task can include multiple instances of an activity;

"Singletask" and "singleinstance" only generate one instance and are the root element of the task.

Singletop requires that if an instance of the activity to be created at the top of the stack is created when the intent is created, the intent will be sent to the instance without creating a new instance.

Whether other activities are allowed to exist in this task

"Singleinstance" occupies only one task, and other activities cannot exist in that task;

If it starts a new activity, regardless of the launch mode of the new activity, the new activity will run in other tasks (like adding the flag_activity_new_task parameter ).

The other three modes can coexist with other activities.

Whether to generate a new instance every time

"Standard" generates a new activity instance for each intent startup;

If the activity of "singletop" is at the top of the stack of the task, no new instance of the activity will be generated, and the instance at the top of the stack will be directly used. Otherwise, the instance of the activity will be generated.

For example:

Now the task stack element is A-B-C-D (D at the top of the stack), at this time to d send a start intent, if D is "standard", then generate a new instance of D, stack to a-B-c-d.

If D is singletop, no new instance of D will be produced and the stack status remains A-B-C-D

If intent is sent to B at this time, a new instance of B will be generated no matter whether the launchmode of B is "standard" or "singletop, stack status changes to a-B-c-d-B.

"Singleinstance" is the only activity in the stack where it is located. It will be reused every time.

If "singletask" is on the top of the stack, intent is accepted. Otherwise, the intent will be discarded, but the task will still return to the foreground. When an existing activity instance processes a new intent, the onnewintent () method is called. If an intent generates an activity instance, the user can return to the previous status through the back key; if an existing activity is used to process this intent, you cannot press the back key to return the previous status.

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.