C1CTech

Activity Lifecycle in Android

This tutorial is about Activity in Android, Activity Lifecycle, Real example use-cases of Activity Lifecycle.

Activity

In Android, an Activity is referred to as one screen in an application. An activity provides the window in which the app draws its UI. An Android app consists of one or more screens or activities.

For example, when we open our Gmail application, then we see our emails on the screen. Those emails are present in an Activity. If we open some particular email, then that email will be opened in some other Activity.

Activities in the system are managed as activity stacks. As a user navigates throughout an app, Android maintains the visited activities in a stack, with the currently visible activity always placed at the top of the stack.

Activity Lifecycle

An Android activity undergoes through a number of states during its whole lifecycle.

An activity has essentially four states:

Running : Activity is visible and interacting with the user.

Paused : Activity is still visible, but no longer interacting with the user.

Stopped : Activity is no longer visible.

Killed : Activity has been killed by the system (low memory) or its finish() method has been called.

To handle these states we should override these seven callbacks onCreate, onStart, onStop, onPause, onResume, onRestart, and onDestroy in our Activity.

The following diagram shows the whole Activity lifecycle:

Activity Lifecycle Methods

The Activity lifecycle consists of 7 methods:

onCreate()
onRestart()
onStart()
onResume()
onPause()
onStop()
onDestroy()

Activity Lifecycle Use Cases 

Now, let’s see real-life use-cases to understand the lifecycle of an activity.

UseCase 1: Open an Activity and press the Back button

When you first time opens an app or activity, onCreateonStart, and onResume callbacks will be triggered. 

onCreate -> onStart -> onResume

Now onPauseonStop and onDestroy callbacks have triggered, as soon as we press the back button.

onPause -> onStop -> onDestroy 
UseCase 2: Open an activity and press the Home button

If we press the Home button, our app will be minimized. But it will not be killed. So rather than onDestroy method call, onPause and onStop will be called.

onPause -> onStop 

Now If we reopen the activity again,  onRestart, onStart, and onResume callbacks will be triggered.

onRestart ->onStart -> onResume  
UseCase 3: Open an Activity and Lock the Screen

If we simply turn off the screen (press the power button) while using the app, onPause and onStop methods are called. 

onPause -> onStop  

If we unlock the screen, onRestart, onStart, and onResume callbacks will be triggered.

onRestart ->onStart -> onResume 
UseCase 4: Open an Activity and Phone Ringing

Now if an incoming call comes while running activity and if we receive that call, in this case, onPause and onStop callbacks will be triggered.

onPause -> onStop 

If we disconnect the call, onRestart, onStart, and onResume callbacks will be triggered.

onRestart -> onStart -> onResume 
UseCase 5: Kill the app from the recent app’s tray

When we kill the app from the recent app’s tray, onPauseonStop and onDestroy callbacks will be triggered. 

onPause -> onStop -> onDestroy 

Here onDestroy will kill the instance of the activity. So now when we reopen the activity, it will call onCreate and not onRestart to start the activity.

UseCase 6: Move from one activity to another

In this use case, we will see what happens when we move from one activity to another (let’s say Activity A to Activity B)

When Activity A will open, the following states are called initially,

onCreate -> onStart -> onResume

Let’s say on click of a button we opened Activity B. While opening Activity B, first, onPause will be called for Activity A and then,

onCreate -> onStart -> onResume

will be called for Activity B and then finally onStop of Activity A will be called.

Now, when we press the back button from Activity B to Activity A, then first, onPause of Activity B is called and then,

onRestart -> onStart -> onResume

gets called for Activity A and it is displayed to the user. Here you can see onRestart gets called rather than onCreate as it is restarting the activity and not creating it.

Then after onResume of Activity A is called, the following methods of Activity B are called and now Activity B is destroyed (removed from activity stack).

onStop -> onDestroy  
UseCase 7: Activity’s  orientation change

When we change the orientation of an Activity while using the app, the following methods get called.

onPause -> onStop -> onDestroy 

After onDestroy, the instance of an Activity gets destroyed and a new instance of activity gets created by calling the following methods.

onCreate -> onStart -> onResume

 

Exit mobile version