Javatpoint Logo
Javatpoint Logo

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.

Activity States

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.

Xamarin.Android Activity LifeCycle

States are divided into four groups:

  1. Active or Running: When the Activities are in the foreground, they can be considered as Active or Running. Active or Running state is also known as the top of the Activity stack. Running state is known as the highest priority activity in Android. This activity can be terminated in an extreme situation when the activity tries to use more memory than the available memory in the operating system. In this case, UI becomes unresponsive.
  2. Paused: When the device goes to sleep, but activity is still visible but partially hidden by a new, non-full sized or transparent activity, in that case, activity is considered Paused. Paused Activities are still alive, i.e., they maintain all the state and member information and will remain attached to the window manager. Paused is the second-highest priority activity in Android. This activity terminates only by the operating system. This activity satisfies the resource requirements, which is needed to keep the activity stable and responsive.
  3. Stopped/Background: Activities which are entirely covered by another activity can be considered stopped or continue in the background. Stopped activity tries to retain their state as long as possible, but stopped activity always considered as the lowest priority activity of all the three states. The operating system can terminate activities in this state to meet the resource requirement for the highest priority activity.
  4. Restarted: In Android, this can be possible for the activity to be removed from memory from paused to stop in the lifecycle. If we want to navigate back to activity, it must be restarted. Restarted would be restored to its previously saved state and then display to the user.

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:

Xamarin.Android Activity LifeCycle

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:

  • Creating Views
  • Initializing Variable
  • Binding the static data to the lists

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:

  • Ramping up the frame rates.
  • Starting the animations
  • Listening to the GPS update
  • It will display the relevant alert such as dialogs or alerts.
  • Wire up the external event handler.

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:

  • Commit the Unsaved changes to persistent data.
  • Consume the resources it should clean up or destroy other objects.
  • Ramp down the frame rates and pause the animations.
  • Unregister the external event handler or notification handler. This must be done to prevent memory leaks in the activity.
  • If activity displays any dialog or alerts, they must be cleaned up with the. Dismiss method.

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.

  1. OnResume will be called if the activity returned to the foreground.
  2. OnStop will be called when the activity is placed in the background.

OnStop: When the Activity is not visible, in that case, we will use OnStop. This activity happens in the following scenario:

  1. When a new activity is starting.
  2. When the existing activity comes back to the foreground.
  3. When an activity is destroyed.

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.

Wrap Up

The Android Activity Lifecycle provides a powerful framework for state management activities within an application, but it can be difficult to understand and implement.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA