The Android Activity Lifecycle


Everything has a lifecycle. You have to believe it's going to change. - Les Wexner

Source: https://www.famouslogos.us/android-logo/

An activity is the backbone of most Android development work and its lifecycle is something an Android developer encounters every day of his life. Being well aware of these and understanding them well will definitely help solve many problems an Android developer encounters. This blog post gives a brief introduction about what an activity is and the lifecycle it goes through.

ACTIVITY

Before we go about understanding what and how the Android activity lifecycle functions, we must first know what an activity is. Activity is actually a class and is a fundamental component of an Android application as the code is initiated in an instance of the Activity class by invoking callback methods which will be touched upon shortly. The different callback methods correspond to the different activity lifecycle stages. The activities thus serve as an entry point for the user to interact with the application as each basically represents a screen of the app. There may thus exist multiple activities of the app which together form the user experience of the application. There exists a main activity which launches the first screen of the app and then all the activities which are actually loosely coupled interact with each other and also with activities of other apps to deliver an enriching experience to the user.

ACTIVITY LIFECYCLE

Now, let's turn our focus to what this blog post is all about, the Activity Lifecycle. Just like every process/product has its own lifecycle, so does an activity. It illustrates the different states that an activity instance transitions through when an app runs. Various callbacks mainly onCreate(), onStart(), onResume(), onPause(), onStop() and onDestroy() informs the activity that its state has changed. These callback methods can also be used to define how the activity must behave whenever the state of the activity changes, for instance when a user leaves it. This helps in increasing the durability of the app and enhancing the performance of the app, and thus ultimately in reducing the crashes the app may suffer from. The following figure completely illustrates the entire lifecycle and thus the states an activity goes through.

The Android Activity Lifecycle
Source: https://developer.android.com/

Now, depending on the complexity of the app and the activity, the developer can decide upon which callbacks are needed to be implemented. The following section briefly describes the different callback methods.

LIFECYCLE CALLBACK METHODS

onCreate()

This is invoked when the activity is first created and the activity enters the Created state. Here, the basic operations which must be performed only once during the entire activity's life must be performed. It also receives a Bundle object which contains the activity's previously saved state, if it exists. This is where the layout file for the activity can also be declared as that is something which is required as soon as the activity is created and is needed to be linked to it only once throughout its entire life. This is one callback which must be implemented in any case. Once this method finishes its execution, the activity enters the Started state followed by the Resumed state and the onStart() and onResume() callbacks are invoked in quick succession respectively.

onStart()

This is invoked when the activity enters the Started state and is made visible to the user while preparing itself so that the user can start interacting with it. This method must contain the code which maintains the interface and keeps track of the changes occurring on it. This callback is followed by the activity entering the Resumed state and thus, the onResume() callback is invoked following this method.

onResume()

This is invoked when the app comes to the foreground and the user can start interacting with the app. This method is called either when the activity starts for the first time or when the activity resumes its functioning and thus, all the necessary operations like initializing and getting control of the required resources must be performed during this callback. This is also one of the callbacks which should be implemented for the proper functioning of the app. The state of the activity remains the same as long as nothing happens to take the focus away from the app, like switching between apps, answering calls, etc. Whenever such an event occurs, the activity enters the Paused state and the onPause() callback is invoked.

onPause()

This is invoked when the user leaves the activity and thus the app, but the latter is still visible to the user, like for instance when a dialog opens up and the app is still partially visible. Here, the activity instance is kept in memory for easy resuming and to avoid re-initialization of components. This is where the various resources which aren't required by the user at that time are released. The execution of this method is very brief and thus, heavy-load operations like save operations, etc. must not be performed during this method, as it may result in the incomplete execution of such operations. The activity remains in the Paused state until it either resumes, which invokes the onResume() callback or becomes completely invisible to the user, which invokes the onStop() callback.

onStop()

This is invoked when the activity is no longer visible to the user and has entered the Stopped state. All the resources which aren't needed by the app are released here, along with the ones which may leak memory. This is also important because the system may kill the process hosting the app without invoking the onDestroy() callback, which actually takes care of decommissioning all the resources. This is also the method where all the heavy-load CPU-intensive operations must be performed. Here too, the activity instance is kept in memory for an easy restart and to avoid re-initialization of components. Even if the system destroys the process while it is stopped, the system retains the state of the View objects in a Bundle object and restores them later when required. After being in this state, the activity either restarts using the onRestart() callback or finishes by invoking the onDestroy() callback.

onDestroy()

This is invoked before the activity is destroyed and is the final call that the activity will receive. This may be invoked either because finish() was called or because the system is temporarily destroying the process hosting the activity to save memory. This callback releases all the resources that have not yet been released by earlier callbacks, like onPause() or onStop().

INTERACTION BETWEEN ACTIVITIES

When one activity starts another, both experience transitions in their respective lifecycles. The first one enters the Paused or Stopped state, and the other gets Created. The first activity isn't completely stopped before the second one is created. The sequence of lifecycle callbacks which get invoked here is quite predictable and thus such transitions are easily manageable. The order of operations is:
  • First activity invokes onPause().
  • Second invokes onCreate(), onStart() and onResume() in sequence.
  • Finally the first invokes onStop() if it is no longer visible to the user.

These are really simple concepts to bear in mind while making an Android app but are really crucial and this is what differentiates an amazing app from a good app. I am not telling you that it is going to be easy, but it is surely going to be worth it. And always remember,

Practice is the best of all instructors - Publilius Syrus


Source: http://ark.media.mit.edu/











Comments

Popular posts from this blog

Touch Typing

Argumentum Ad Verecundiam (Appeal To Authority)

Learning Linux The Hard Way (LLTHW)