First, preface
People who have used Android phones will definitely find a phenomenon that when you put an app in the background, after a while, you'll notice that the app is reopened, but the relevant data is not lost. You can see that the app's "Life" is in the hands of the system, rather than the program developed on Windows.
Ii. life cycle of activities here we use a graph to help us understand:
Of course, many people still do not understand this picture, below we will introduce:
1.OnCreate
This method is the first event to be executed after an activity is opened, and certainly a method that we must rewrite, which is roughly responsible for the following things:
L Create a view
L Initialize variables
L BIND a static variable to a list
OnCreate has a bundle-type parameter bundle, which is important because we know that the application's life cycle is not owned by itself, so it can be terminated at any time,
So we're going to be able to save the data for this intermediate user operation and recover it the next time we open it, and the bundle parameter here is used to pass the data saved in the previous instance,
This restores the user's last state. The following code simply demonstrates how to get the data from the bundle:
1 protected override void OnCreate (bundle bundle) 2 {3 base. OnCreate (bundle); 4 5 string extrastring; 6 bool Extrabool; 7 8 if (bundle! = NULL) 9 {Ten intentstring = Bundle. GetString ("myString"); Intentbool = bundle. Getboolean ("Mybool"); }13 Setcontentview (Resource.Layout.Main); 14}
When the OnCreate is finished, the OnStart method is executed shortly thereafter.
2.OnStart
OnStart will be called by the system after oncreate, which generally only needs to perform special tasks before the view appears, such as updating the values displayed in the view. The Onresume method is executed after the method is finished.
3.OnResume
This method is triggered when the activity is ready to start interacting with the user, and is typically overridden only when the following actions are required:
L Start Animation
L Start monitoring GPS updates
L display some related prompts and dialogs
L Register for radio monitoring
As a demonstration, here is the code to initialize the camera:
1 public void Onresume () 2 {3 base. Onresume (); 4 5 if (_camera==null) 6 {7 //Initialize 8 }9}
Onresume is very important, especially after triggering onpause. The user also cuts back to the application, then the onresume needs to be restored. So then there is the OnPause event.
4.OnPause
This method is triggered when the activity is switched to the background, generally we need to do the following things in the event:
L Save User uncommitted data
L Close or clear referenced resources
L Write off the broadcast
L If there is a prompt or dialog box that is being displayed, you must use it. Dismiss () to clear.
As a demonstration, the following code will release the camera resource:
1 public void OnPause () 2 {3 base. OnPause (); 4 5 if (_camera! = null) 6 {7 _camera. Release (); 8 _camera = null; 9 }10}
5.OnStop
When the activity is not displayed to the user for a long time, it is usually triggered by the following reasons:
L When a new activity is opened and overwritten by the activity
L When an existing activity is switched to the foreground
L When the activity is destroyed
OnStop not always be executed, if the memory is low, the system will not execute the event, but instead directly shut down the application, so everyone in the OnPause event will be stored in the parameters and so on. and cannot rely on the event.
6.OnDestroy
This event is the last one in the entire activity life cycle, but in many cases it is not performed to this event, so it is seldom overridden.
7.OnRestart
This event is triggered when the user switches the user to the background through the home button and then opens the app. This method is rarely overridden.
Section:
Through the above introduction, we can understand that not all events in the activity are useful, among which the more commonly used is oncreate,onresume,onpause, of course, the following also describes the events specifically used to save the user state.
Third, the life cycle of state management
First we use a picture to describe:
From this graph we can see that the Onsaveinstancestate method will be triggered when the application is restored to the destruction process, and the Onrestoreinstancestate method will be triggered when the app is opened. Let's take a practical example to illustrate the following:
Let's start with a simple example of what happens if we don't save the state of the current user:
First create a new Android project, then open Main.axml, drag a text (Large), and drag a button below, set the button's text to add, and then generate it again.
Open the MainActivity.cs file and write the following code:
1 [Activity (Label = "Activity_liftcycle", Mainlauncher = true, Icon = "@drawable/icon")] 2 public class Mainactiv Ity:activity 3 {4 int count = 1; 5 6 private TextView TV; 7 8 protected override void OnCreate ( Bundle bundle) 9 {Ten base. OnCreate (bundle); Setcontentview (Resource.Layout.Main); TV = Findviewbyid<textview> ( RESOURCE.ID.TEXTVIEW1), TV. Text = count. ToString (); Button btn = findviewbyid<button> (Resource.Id.button1); btn. Click + = (E, s) =>17 { count++;19 TV. Text = count. ToString (); };21 }22}
There is no explanation here, and we all know that it is an accumulation. Now we run the F5 and then click Add several times as follows:
Then we select the simulator and press Ctrl + F12 to find out if the simulator is flipped, but you will also find that the number becomes 1:
If you're using an app that looks like this after flipping, you're going to be pretty weird. Of course you might think of using static variables, but you've considered a reality,
Save a good, if a lot of it. That consumes more memory, and it doesn't make sense. So here we need to rewrite the Onsaveinstancestate method above to
To save the current user's data, the following changes the code as follows:
1 [Activity (Label = "Activity_liftcycle", Mainlauncher = true, Icon = "@drawable/icon")] 2 public class Mainactiv Ity:activity 3 {4 int count = 1; 5 6 private TextView TV; 7 8 protected override void OnC Reate (Bundle bundle) 9 {ten base. OnCreate (bundle); Setcontentview (Resource.Layout.Main); 12 13//If present, the status before restore if (bu Ndle! = null). {. Count = bundle. GetInt ("_count");}18 TV = Findviewbyid<textview> (Resource.Id.textView1); 20 Tv. Text = count. ToString (); Button btn = findviewbyid<button> (Resource.Id.button1); Click + = (E, s) =>23 {count++;25 TV. Text = count. ToString ();};27}28 protected override void Onsaveinstancestate (Bundle outstate) 30 {base. Onsaveinstancestate (outstate); 32//Save current status Outstate.putint ("_count", count); 34}35}
We then click Add:
Then flip:
There is also a onrestoreinstancestate that can be used to restore the state, except that it will execute after OnStart. This means that it is done after all initialization, and that the parameter of the method is consistent with the parameters in OnCreate, so it is not common for this method to restore the state.
Life cycle of xamarin.android activities