Android and androidsdk
Two types of destruction
FirstIs normal, for example, the user presses the Back button or the activity calls the finish () method;
AnotherBecause the activity is in the stopped state and has not been used for a long time, or the foreground activity requires more resources, the system will shut down the background process to restore some memory.
Note that one of these situations is screen rotation. When a user rotates the mobile phone screen, the activity is destroyed and re-created every time.
In the second case, although the actual activity instance has been destroyed, the system still remembers its existence. When the user returns it, the system will create a new instance to replace it. here we need to use the data stored when the old instance is destroyed.The data is called "instance state" and is a collection of key-value pairs in the Bundle object.
By default, the system saves each View object (such as the text in the EditText object and the position of the scroll bar in the ListView), that is, if the activity instance is destroyed and rebuilt, the layout status will be restored to the previous status without your encoding.
However, if your activity needs to restore more information, such as the member variable information, you need to write it yourself.
OnSaveInstanceState ()
To store additional data, you must overwrite the onSaveInstanceState () callback function ().
The system will call this function when the user leaves the activity and pass it a Bundle object. If the system needs to recreate the activity instance later, it will pass the same Bundle object to onRestoreInstanceState () and the onCreate () method.
OnSaveInstanceState () (process 1). If the activity is destroyed but the same instance needs to be created, the system will pass the status data in process 1 to onCreate () and onRestoreInstanceState () (2 and 3 marked in the figure ).
Store Activity status
When the system stops the activity, the system calls onSaveInstanceState ().Key-value pairsStorage.
The default implementation stores the status of the view series of the activity, such as the text and scroll bar positions.
To store additional information, you must implement onSaveInstanceState () and add a key-value pair to the Bundle object.
static final String STATE_SCORE = "playerScore";static final String STATE_LEVEL = "playerLevel";...@Overridepublic void onSaveInstanceState(Bundle savedInstanceState) { // Save the user's current game state savedInstanceState.putInt(STATE_SCORE, mCurrentScore); savedInstanceState.putInt(STATE_LEVEL, mCurrentLevel); // Always call the superclass so it can save the view hierarchy state super.onSaveInstanceState(savedInstanceState);}
Remember to call the implementation of the base class to implement the default implementation.
Restore Activity status
When the activity is rebuilt, the activity needs to be restored Based on the status information in the Bundle. The onCreate () and onRestoreInstanceState () callback functions receive this Bundle.
Because the onCreate () method is called every time you create a new activity instance or recreate an instance, you must first check whether Bundle is null. If it is null, it indicates that you want to create a brand new object instead of recreating the object that was destroyed last time.
For example, the onCreate () method can be written as follows:
@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Always call the superclass first // Check whether we're recreating a previously destroyed instance if (savedInstanceState != null) { // Restore value of members from saved state mCurrentScore = savedInstanceState.getInt(STATE_SCORE); mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL); } else { // Probably initialize members with default values for a new instance } ...}
In addition to restoring the status in onCreate (), you can also choose to implement it in onRestoreInstanceState (). This function is called after onStart.
The system calls onRestoreInstanceState () only when data is to be restored. Therefore, you do not have to check whether the Bundle is null.
public void onRestoreInstanceState(Bundle savedInstanceState) { // Always call the superclass so it can restore the view hierarchy super.onRestoreInstanceState(savedInstanceState); // Restore state members from saved instance mCurrentScore = savedInstanceState.getInt(STATE_SCORE); mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL);}
Do not forget to call the base class implementation.
I am the dividing line of tiantiao
Android: id
Android: id = "@ + id/layout01": The layout01 variable is defined and is automatically written into R. java, in R. the internal class id is generated in the java file, which can be called in the main program. id. layout01 to obtain the layout variable entity.
Android: orientation = "vertical": the vertical layout. Horizontal for horizontal layout
Android: id
Android: id = "@ + id/layout01": The layout01 variable is defined and is automatically written into R. java, in R. the internal class id is generated in the java file, which can be called in the main program. id. layout01 to obtain the layout variable entity.
Android: orientation = "vertical": the vertical layout. Horizontal for horizontal layout