THE ANDROID ACTIVITY LIFECYCLE

Cal Ugonabo
2 min readMay 31, 2021

Like every living thing, an android application activity has a life cycle which starts the moment it is created and ends when it is destroyed. see image below.

source: developer.android.com

This image summarises the android application lifecycle. I will go through the stages one after the other.

A visible activity is an activity that appears on the screen of the user

An app has focus when it is visible and the user can directly interact with it.

  1. onCreate(): This is the first stage in the lifecycle of an android app, this must be implemented, the others call backs listed below are optional. Just like in humans, an app can only be created once so this method is called only once and usually contains code to start up your application and those that are used once in the entire life of the application. After this is called, the app is neither visible nor has focus.
  2. onStart(): This is the next callback to be called and it basically makes the app visible, typical code that exists in this method are code that maintains the UI, once this method finishes, the activity enters the Re state, and the system invokes the onResume() method.
  3. onResume(): This callback gives the activity focus and enables it to interact with the user, this focus can be taken away if the screen goes off, if the activity is closed or the user navigates to the home screen among other scenarios. At this point, if a call comes in, the onPause() will be invoked as the activity will lose focus but it will still be visible, after the call, the onResume() will be invoked which will give the activity focus.
  4. onPause(): This is called when the activity no longer has focus, it may or may not be visible but has certainly lost focus. The activity can go on to be destroyed or be resumed. It is advisable to not perform heavy database or network calls in this method because of the amount of time it takes.
  5. onStop(): This is the opposite of onStart(), this callback is invoked when the activity is no longer visible to the user, e.g if the user clicks on the home screen, the onPause() is called followed by onStop(). It is advisable to perform CPU-intensive shutdown operations here.
  6. onDestroy(): This method is called just before the activity is destroyed, this destruction can be caused by the user closing the activity, the finish() method being called or system configuration change whereby the activity has to be restarted. Basically when i shut down an activity, this callback is invoked.

This is basically the various stages in the lifecycle of an activity.

credit: developer.android.com

--

--

Cal Ugonabo
0 Followers

I am a final year student of Computer science, I am learning Android development and I like to write about what I have learnt so far