Xamarin.Android Activity LifeCycle
Activities are the building block of Android applications, and they can exist in different states. Activity LifeCycle begins with the initialization and ends with termination. Activity LifeCycle includes many states between the initialization and termination of the application. Activity is a single page in the Android app where user can perform the interaction.
AndroidOS Activities are based on their state. Android states help the Android to identify the activities which are no longer in use. Android states allow the Operating System to reclaim memory and resources.
The diagram shows the states of the activity that can go through during its lifetime.
States are divided into four groups:
Activity LifeCycle Method
When a user navigates through the Android app, a series of events occur. For example, when we launch an app, such as the Facebook app, it starts and appears on the foreground for the user, onCreate () → onStart () → onResume ().
If any other activity starts, For example: when a phone call arrives, the Facebook app will go to the background, and the call arrives in the foreground. Now we have two processes going on.
When phone call ends, the Facebook app returns to the foreground. Three methods are called.
Xamarin.Android framework provides a powerful model for managing the activity states within the application. When the state of the activity is changed, activity is notified by the operating system, which calls the specific method on that activity.
Here are the few methods which specify the relationship to the Activity lifecycle:
As a developer, we can handle the changes in the state by overriding the methods within the activity. Here, we have to notice that all the lifecycle methods are called on the UI thread and block the operating system from performing the next UI work. It will hide the current activity and display the new activity. Any long-running tasks should be executed on the background thread.
Here are the lifecycle methods and their uses:
OnCreate: OnCreate is the first method which is called when any activity is created. OnCreate is always overridden to perform any initialization which is required for any activity such as:
OnCreate takes the Bundle parameter, which is a dictionary for storing and passing the information of the state and object between the activities. If the bundle is not null, this indicates that the activity is restarting and it should restore its state from the previous one.
Once the OnCreate finished, Android will start the OnStart.
OnStart: When OnCreate is finished, system will call OnStart. Activities may override this method if there is a need to perform any task before any activity is visible. OnStart includes such as, refreshing the current values of the views within the activity. Android will call OnResume method after OnStart.
OnResume: When any activity is ready to start the interaction with the user, system will call OnResume. The activity should override this method to perform the tasks. These tasks are:
Here we will write the code to shows how to initialize the camera.
OnResume is important because any operation which is done in OnPause should be undone in OnResume. It is the only method which executed after OnPause when we start any activity.
OnPause: OnPause is called when the system is going to put the activity into the background or when the activity is not visible. Activities should override the OnPause method if there is a need to:
Example: In this example, we release the camera. An Activity cannot use it during the paused.
Here are the two lifecycle methods which will call after OnPause.
OnStop: When the Activity is not visible, in that case, we will use OnStop. This activity happens in the following scenario:
OnStop cannot be called in the situation of low memory when Android needs memory resources and unable to do the activity in the background. This is the reason we cannot rely on OnStop when we are planning to terminate any activity.
The next lifecycle methods that can be called after this will be OnDestroy if the activity is running away, and OnRestart will be used if the activity is returning to interact.
OnDestroy: OnDestroy is a final method called on an activity instance, before it is destroyed and completely removed from memory. In extreme situations, Android may kill the hosting process of the activity, resulting in OnDestroy not being implemented. Mostly activities will not perform this method because primarily clean up and shut down has been done in the OnPause or OnStop method.
The OnDestroy method is usually overridden to clean up long-running resources that can leak resources.
Example of this is the background threads that were started in OnCreate. There will be no lifecycle methods after the activity is destroyed.
OnRestart: OnRestart is called after stopping the activity, before resuming it.
Example of this would be when the user presses the home button while on an activity in the application. When this happens, OnPause and then OnStop is called, and the activity is moved to the background but not destroyed. If we want to reinstall the application, then using Task Manager or a similar application, Android would call the Activity's OnRestart method.
OnStart will be the next lifecycle method to be called after OnRestart.
The Android Activity Lifecycle provides a powerful framework for state management activities within an application, but it can be difficult to understand and implement.