Xamarin – Activity Lifecycle and Its Methods in Android
We have learned about the Activity. Now when you create an application in Android, you find that Activity1.cs has an OnCreate overridden method. This is one of the activity’s lifecycle methods.
In this post we are going to see, what the other methods are and when to use them.
The following figure briefly explains about the activity states.
Activity Life Cycle Methods
The following figure briefly describes itself.
This method is called when the activity is first created.
This is where you should do all of your normal static set up — create views, bind data to lists, and so on.
This method is called just before the activity becomes visible to the user.
Followed by OnResume() if the activity comes to the foreground, or OnStop() if it becomes hidden.
This method is called just before the activity starts interacting with the user.
At this point the activity is at the top of the activity stack, with user input going to it.
Always followed by OnPause().
This method Called after the activity has been stopped, just prior to it being started again.
Always followed by OnStart()
This method is called when the system is about to start resuming another activity.
This method is typically used to commit unsaved changes to persistent data, stop animations and other things that may be consuming CPU, and so on.
It should do whatever it does very quickly, because the next activity will not be resumed until it returns.
Followed either by OnResume() if the activity returns back to the front, or by OnStop() if it becomes invisible to the user.
This method is called when the activity is no longer visible to the user.
This may happen because it is being destroyed, or because another activity (either an existing one or a new one) has been resumed and is covering it.
Followed either by OnRestart() if the activity is coming back to interact with the user, or by OnDestroy() if this activity is going away.
This method is called before the activity is destroyed.
This is the final call that the activity will receive.
It could be called either because the activity is finishing, or because the system is temporarily destroying this instance of the activity to save space.
The following is an implementation of the above mentioned methods in Activity.
Hope this article helps, thanks for reading it.