Android Elite Biography-eighth chapter: Activity and activity Call stack analysis
At the beginning, we state that activity,activity is the core component of the entire application user interaction, understanding the activity's working mode, life cycle and management style, is the foundation of Understanding Android, this section is the speaker
- Activity's life cycle and working mode
- Activity Call Stack Management
I. Activity
Activity as the four major formation of the most flat rate of the components, we can see him everywhere, let us first to understand his life cycle
1. Origins
Activity is the user interaction of the first interface, he provides a user to complete the instructions window, when the developer created the activity, by calling Setcontentview to specify a window interface, and based on this, to provide user interaction interface, The system uses activity stacks to manage activity
2. Activity patterns
One of the biggest features of activity is that it has multiple forms, and he can switch freely in many forms to control his life cycle.
At this point, the activity is at the top of the activity stack, visible, and interacting with the user
When the activity loses focus and is placed at the top of the stack by a new non-full-screen activity or a transparent activity, the activity is transformed into a qaused form, which is the ability to interact with the user, all state information, and member variables remain, The system will only be recycled if the system memory is in the polar state.
If an activity is completely covered by another activity, the activity enters the stop pattern, and he is not visible at this time, but retains all the state and member variables.
When the activity is recycled by the system or the activity has never been created, the activity is in a killed state,
This shows that the user's different operations, will let the activity into four different states, and developers can only control their life, but can not control their death
3. Life cycle
Google gave us a picture of the activity's life cycle, and he wanted the activity to be controlled by the developer, not a runaway Mustang.
Developers do not necessarily have to implement all the life cycle methods, but must know the meaning of each life cycle, so that we can better control the activity, so that he can achieve the desired effect.
@Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_main); }@Override protected void OnStart() {Super. OnStart (); }@Override protected void Onresume() {Super. Onresume (); }@Override protected void Onrestart() {Super. Onrestart (); }@Override protected void OnPause() {Super. OnPause (); }@Override protected void OnStop() {Super. OnStop (); }@Override protected void OnDestroy() {Super. OnDestroy (); }
-1.activity Start and destroy process
After the system calls the OnCreate method, it calls OnStart immediately, then continues to call Onresume to run the graph, and finally stops at the Onresume state, completes the boot, The system calls OnDestroy to end an activity's life cycle and let him destroy the kill state.
The above is the process of initiating and destroying an activity.
- Creating a basic UI element in OnCreate
- OnPause and OnStop: Clear acvtivity resources to avoid waste
- OnDestroy: Because the reference is destroyed when the activity is destroyed, and the thread does not, the open thread is cleared
-2.activity pause and Resume process
When the activity portion of the top of the stack is not visible, the activity is inverted into the onpause
- OnPause: Release system resources,
- Onresume: Need to reinitialize onpause freed resources
-3. The stopping process of activity
When the activity part of the top of the stack is not visible, there are actually two possible follow-up, which can be seen from the partial invisible, that is, the recovery process, from partially invisible, that is, stopping the process, and the system calls OnPause when the current activity is not visible.
-4.activity Re-creation process
Finally, let's look at how the activity is recreated, if your system is in a stop state for a long time, and the system needs more memory or system memory is tense, the system will recycle your activity, and the system to compensate you, Will save your activity state through the Onrestoreinstancestate () method to the bundle, and of course you can add extra key-value pairs to save these states, and when you re-need to create this activity, The saved bundle object is passed to the activity's Onrestoreinstancestate () method to go with the OnCreate method, which is also the source of the OnCreate important parameter--saveinstancestate.
But one thing to note here is that the Savedinstancestate method is not called every time the activity leaves the foreground, and is not invoked if the user finishes using the Finish method, and the Android system has already implemented the cache state of the control by default. Once to reduce the cache logic that developers need to implement
Two. Android Task stack Introduction
An Android app feature is typically split into multiple activity, and each activity is connected through intcnt, while the Android system, which holds the entire app's activity through the stack structure, is the initiator of the entire task stack. A reasonable task scheduling stack is not only the guarantee of performance, but also the basis of providing performance.
When an app launches, if the app's task stack does not exist in the current environment, a task stack is created and the activity that the app launches is managed in this task stack, which is also called a task, which represents a collection of several acnvity. They are grouped together to form a task. In addition, it is important to note that the activity in a task can come from different apps, and the acnvity of the same app may not be in a task.
On the stack structure, I'm sure you're not too familiar with the linear table of one by one LIFO (Lastin first Out). Determines the state of the acavity based on the position of the activity in the current stack structure. Take a look at the normal Android task stack, and when one activity initiates another activity, the newly launched acnvity will be placed at the top of the task stack.
End, and is active, while the activity that starts it is retire, but remains in the task stack, in a stopped state, when the user presses the return key or calls the finish () method, the system removes the top activity, leaving the back acnvity to resume active state. Of course, the world cannot always be so "harmonious", can give the activity to set some "privileges", to fight
Break this "harmony" pattern. This privilege is set by the attribute Android:1aunchmode in the Androidmainifest file or by the flag of intent.
Three. Androidmanifest Boot mode
Let's take a look at the privileges, Android developers can design a total of four boot modes in the Androidmanifest file
- Standard
- Singletop
- Singletask
- SingleInstance
1. Standard
The default startup mode, if you do not specify the activity's startup mode, use this mode to start activity, each time you click Standard mode to create the activity, will create a new mainactivity overlay on the original activity,
2.singleTop
If the specified activity is started in Singletop, then at startup, the system will determine whether the current top activity is the one that is to be started, if it is not a new activity, if not create a new activity, This mode is usually used to receive the message after the display interface, QQ received after receiving the message pop activity, if you come to 10 at a time, can not play 10 times, this startup mode of
Although this startup mode cannot create a new instance, the system will still invoke the Onnewintent () method when the activity is started, for example, if there is an ABC three activity in the current task stack, and the C startup mode is Singletop, Then start C at this time, then the system will not create an instance of C, but will call C's Onnewintent method, the current task stack is still ABC three activity
3.singleTask
The Singletask mode is somewhat similar to the singletop mode, except that Singletop is the activity that detects whether the top element of the stack needs to be started, and Singletask is to detect the presence of the active activity in the entire activity stack. If it exists, put him on top of the stack and destroy all of the above activity, but this also refers to the activity that launches the entire singletask in the same app, and if other programs start the whole activity in singletask mode, Then he will create a new task stack, but here is one thing to note is that if the activation mode for singletask activity is already in the background of a stack, then after the start, a task stack in the background will be switched to the foreground, with a map of the official website we may better understand
When Activity2 starts Activityy (startup mode is Singletask), his task is switched to the foreground, and when the return key is returned, the activity of the task is returned first, which is more difficult to understand, Let's take a look at the picture.
It can be found that the acnvity created using this pattern is not opened in the new task stack, that is, the open activity is swapped to the foreground, so this startup mode can usually be used to exit the entire application, the main acnvity is set to Singlelask mode, Then in the acnvity to exit to the main acnvity, so that the main acnvity acnvity all destroyed, and then rewrite the Actlvity method of the main onnewintent add a finish to the method and end the last activity.
4.singleInstance
Singieinstance This startup mode works similarly to how browsers work. When you access a browser in multiple programs, open the browser if the current browser is not open, or it will be accessed in the currently open browser. It is stated that the activity for SingleInstance will appear in a new task stack and that there is only one activity in the task stack, for example, if a mainactivity instance is created in the task stack of application A, And the startup mode is singleinstance, if app B also activates mainactivity, then no need to create, two apps share the activity instance, which is often used for interfaces that need to be detached from the program: such as calling emergency calls in SetupWizard, is to use this kind of Kai
Dynamic mode
About Singletop Shao P and singleinstance these two startup modes also require special instructions: If the Startactivityforresulto method is used to start another activityb in a singletop or singleinstance activity, the system will return directly to the Activity_result_ Canceled instead of waiting to return. This is due to the fact that the system has limited the two startup modes at the framework level, because Android developers believe that data cannot be passed by default in different tasks. If you must pass the data, you can only bind the data through intent.
Four. Intent Flag boot Mode
As we have already said, the system provides two ways to set up an activity startup mode, and the following is to set up a start mode of activity by setting flag with intent
Here are some of the commonly used flag
Five. Empty the task stack
The system also provides a way to empty the task stack so that we can talk about a task emptying, which we normally use to empty the task stack with the following properties on the Activity tab
The Cleartaskonlaunch property, as its name implies, clears all activity on the activity every time it returns to activity, and with this property, it is possible for the task to initialize with only one activity at a time.
Finishtaskonlaunch This property and cleartaskonlaunch a bit similar, but cleartaskonlaunch effect on others, and finishtaskonlaunch role in themselves, through this attribute, When you leave the task where the activity is located, the activity will be erased when the user returns.
The Alwaysretaintaskstate property gives the task a gold medal, and if the Activity property is set to true, the activity's task will not accept any purge commands, keeping the state of the current task
Six. Activity task stack Usage
We use the activity task stack of the various startup mode and cleanup method, is to better use the app in actvity, reasonable set acuvity startup mode will make the program run more efficient, better user experience. But the task stack is good, but also can not abuse if excessive use of the activity task stack, it will lead to the entire app stack management confusion, not conducive to the future expansion of the program, and in the easy to appear due to the task stack caused by the display anomaly, such a bug is difficult to adjust. So, Using the Acuvity task stack in an app must be based on the needs of the actual project rather than using the task stack for the task stack.
OK, this chapter is here, this chapter is theory, no demo Oh!
Note Download: Http://pan.baidu.com/s/1c0U7k2W Password: 9v0g
Android Elite Biography-eighth chapter: Activity and activity Call stack analysis