This series is the "first line of Android Code" Learning notes, if there are errors and omissions, please correct me!
In order to write a smooth and coherent program, we need to know the life cycle of the activity.
First, back to the stack
Activities in Android can be stacked. Each start of a new activity, we will overwrite the original activity, and then click the Back button will destroy the top activity. In fact, Android uses tasks to manage activities, and a task is a set of activities that are stored in the stack, also known as the back stack. Default condition
Next, whenever we start a new activity, it will go into the stack in the return stack and be in the top position of the stack. The system always shows the activity at the top of the stack to the user. Also, whenever we press the back key or call the finish () method to destroy an activity, the activity at the top of the stack is stacked.
Ii. four states of activity
1) Operating Status
when an activity is at the top of the stack at the back of the stack, the activity is in the running state.
2) Pause status
when an activity is no longer in the top position of the stack, but is still visible, the activity enters a paused state. For example, an activity in the form of a dialog box occupies only a portion of the middle of the screen, and may still be visible when it is not on top of the stack. The suspended activity is still completely alive, and the system generally does not go back to recycling this activity (because it is still visible, the recycling of visible things will have a bad impact on the user experience), only in the case of very low memory, the system will consider recycling this activity.
3) Stop State
when an activity is no longer at the top of the stack and is completely invisible, it enters a stop state. The system will still save the appropriate state and member variables for this activity, but this is not completely reliable, and when memory is needed elsewhere, the activity in the stopped state may be reclaimed by the system.
4) Destruction Status
when an activity is removed from the return stack, it becomes a destroyed state. The system will generally recycle activities in this State, thus ensuring that the phone has sufficient memory.
Iii. life cycle of activities
First look at the 7 callback methods in the Activity class:
1. OnCreate ()
in each event we rewrite this method, which is called when the activity is first created. We should do the initialization of the activity in this method, such as loading layout, binding event, etc.
2. OnStart ()
This method is called when the activity becomes visible from invisible.
3. Onresume ()
This method is called when the activity is ready to interact with the user. The activity at this point must be at the top of the stack that is returned and is in a running state.
4. OnPause ()
This method is called when the system is ready to start or resume another activity. We usually release some CPU-consuming resources in this method and save some key data, but this method must be executed quickly, otherwise it will affect the use of the new stack top activity.
5. OnStop ()
This method is called when the activity is going to become completely invisible. The main difference between it and the OnPause () method is that if the new activity started is a dialog-box activity, then the OnPause () method will be executed, and the OnStop () method will not execute.
6. OnDestroy ()
This method is called when the activity will be destroyed, and then the state of the activity becomes the destroy state.
7. Onrestart ()
This method is called when the activity is changed from the stop state to the running state, i.e. the activity is restarted.
Based on the callback method above, an activity can be divided into three cycles:
1) Full Life time
The activity between the OnCreate () method and the OnDestroy () method is the complete survival period. In general, an activity completes various initialization operations in the OnCreate () method, while freeing the memory in the OnDestroy () method.
2) Visible lifetime
The activity that is experienced between the OnStart () method and the OnStop () method is the visible lifetime. During the visible lifetime, activities are always visible to the user, even though they may not be able to interact with the user. Through these two methods, we can reasonably manage the resources that are visible to the user. For example, the resource is loaded in the OnStart () method, and the resource is freed in the OnStop () method to ensure that the inactive activity does not consume too much memory.
3) Foreground lifetime
The activity between the Onresume () method and the OnPause () method is the foreground lifetime. In the foreground life period, the activity is always in the running state, at this time the activity is can and the user to each other, we usually see and contact the most also this state of activity.
Iv. onsaveinstancestate () method
If we have a text input box in the mainactivity, now I input a paragraph of text, and then start another, then mainactivity because the system memory is not enough to be recycled, after a while I clicked back key to return to Mainactivity, All the text just entered was gone, because Mainactivity was re-created. To solve this problem, we can Onsaveinstancestate () method:
The Onsaveinstancestate () method carries a bundle type parameter, and the bundle provides a series of methods for saving the data, such as using the Putstring () method to hold the string, saving the integer data using the Putint () method, and so on. Each save method needs to pass in two parameters, the first parameter is the key, which is used to take the value from the Bundle later, and the second parameter is the actual content to be saved.
For example, we can add the following code to save the temporary data in mainactivity:
1 @Override 2 protected void onsaveinstancestate (Bundle outstate) {3 Super . Onsaveinstancestate (outstate); 4 String TempData = "Something you just typed"; 5 Outstate.putstring ("Data_key", tempdata); 6 }
To extract the data, just use the savedinstancestate parameter of the bundle type in OnCreate (), if you haven't previously saved the data with Onsaveinstancestate , Its value is null. The code examples are as follows:
1 @Override2 protected voidonCreate (Bundle savedinstancestate) {3 Super. OnCreate (savedinstancestate);4 Setcontentview (r.layout.activity_main);5 if(Savedinstancestate! =NULL) {6String TempData = savedinstancestate.getstring ("Data_key");7 }8 //...9}
End.
Android Learning Note (v)-The life cycle of an activity