Android Activity: Four boot modes, Intent flags and task stacks (transfer from others blog)

Source: Internet
Author: User

Every interface in Android is an activity, and switching interface operations is actually an instantiation of multiple different activity. How does the individual page jump relationship decide? If you start the ABCD Activiy in sequence, how do I get back to B from d? Here's a look at the four startup modes of acitivity. Before explaining the starting mode, it is necessary to explain the concept of "task stack" first.
Task stack
Each application has at least one task stack, which is used to store activity and functions like a stack of function calls, in order to represent the order in which the activity appears; For example, Activity1-->activity2-->activity3, the task stack is:

Startup mode: The startup mode is simply the activity startup policy, the label's Android:launchmode property setting in Androidmanifest.xml;
Four modes of startup:
(1) Standard: Each time the activity is activated (startactivity), the activity instance is created and put into the task stack; this is the default boot mode of the system;



(2) Singletop: If there is an instance of the activity at the top of the task, the instance will be reused, and the new instance is created and placed on top of the stack (even if the activity instance already exists in the stack, as long as it is not on top of the stack, an instance will be created).
The current activity at the top of the stack is automatically detected each time a new activity is used, whether it is an activity that needs to be referenced, or if it is directly referenced, without creating a new activity, when it does not need to create a new activity object. It calls the Onnewintent () method.


(3) Singletask: If the activity that you want to activate is present in the task stack, you do not need to create it, just put this activity on top of the stack, and pop the activity instances above the activity (eject destroy) This mode can be used to exit the entire application. Set the main activity to Singtask mode, then transfer the activity to the main activity in order to exit, then rewrite the onnewintent function of the main activity and add a finish to the function. (Because the activity is not created, it does not enter the OnCreate function, only into the Onnewintent function).

(4) SingleInstance: If Mainactivity instance is created in the task stack of application 1, if Application 2 also activates mainactivity, then no need to create, two apps to share the activity instance;
This startup mode works similarly to the browsers we use, and we all know that when accessing a browser in multiple programs, if the current browser is not open, the browser opens, otherwise it will be accessed in the currently open browser. This mode saves a lot of system resources because he can guarantee that the activity object to be requested only exists in the current stack.


The above is the activity of the four kinds of startup mode, the title of the intent flags is what ghost?
Flags: Represents the intent flag, commonly used in the activity scene, and it is closely related to the activation mode of the activity.
The flags properties related to this topic are listed below:

Intent.flag_activity_new_task (default)
The default jump type, which re-creates a new activity

Flag_activity_single_top
This flag is equivalent to the singletop in the startup mode, for example: the original stack structure is a B C D, start D in D, the case in the stack is still a,b,c,d. Singletask not the same.

Flag_activity_clear_top
This flag is a bit like the singletask in boot mode, and this flag-activated activity will pop up the stack space on the activity above the activity to be started. For example: The structure in the original stack is a B C D, jump from D to B, the structure of the stack is changed to a B. But unlike Singletask, Flag_activity_clear_top will eject the top of him, but destroy itself, and recreate a new object. The singletask does not recreate the new object, that is, the oncreate is not triggered, only the onnewintent is triggered.

Flag_activity_no_user_action
Onuserleavehint (), as part of the activity cycle, is used when the activity is returned to background because the user wants to jump to another activity. For example, when the user presses the home key, it will be called. For example, if a phone comes in (not a user's choice), it will not be called.
So how does the system differentiate between making the current activity go back to background and using it as the user's choice?
It is determined by whether there is flag_activity_no_user_action in the intent of the newly activated activity that prompted the current activity to retreat to background.
Note: Calling finish () does not call this function when it destroys the activity

Flag_activity_no_history
It means that with this flag activated activity, once exited, it will not exist in the stack, for example! The original is a,b,c this time again C with this flag to start D, D and then start E, this time the stack in the case of A,b,c,e.

Note: When calling to Onnewintent (intent), you need to use setintent (intent) in Onnewintent () to assign intent to activity. Otherwise, the subsequent getintent () is to get old intent.

Android Activity: Four boot modes, Intent flags and task stacks (transfer from others blog)

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.