The Android Activity Lifecycle

In this tutorial we’ll explore the Android Activity Lifecycle.

An Android Activity is a screen or a window that is presented through the UI – in other words, it is a screen or a window which the user can interact with.
An activity typically contains UI widgets like Buttons, TextViews etc.

In the Android environment, there is a total of 7 different states which an activity can go through.
All of these states are defined through methods provided by the Android framework, and are commonly referred to as lifecycle methods.

Before describing each of the 7 lifecycle methods, have a look at this slick flow chart provided by Google, describing the Android activity lifecycle.

android activity lifecycle

Source: https://developer.android.com/guide/components/activities/activity-lifecycle

For some, the flow chart can be kind of messy, so we’ve also created a simple table with concrete information about each of the lifecycle methods.

If you're serious about Android development, I highly recommend the Google-certified Android App Development Certification Course

Use the coupon code LIMITED25 to get 25% off 😉
MethodDescription
onCreateCalled when the activity is first created.
onStartCalled when the activity is presented through the UI.
onResumeCalled when the activity starts interacting through the UI (receiving input etc)
onPauseCalled when the activity is no longer visible in the foreground
onStopCalled when the activity is no longer visible to the user.
onRestartCalled when the activity has been stopped and restarted, without onDestroy being called.
onDestroyCalled before the activity is being destroyed

Live Example of the Android Activity Lifecycle

To get a clear understanding of how an activity works internally, you should create an example application and look for the callback methods.
Let’s fire up Android Studio and do just that!

At first, start a new Android project, and initiate your project with an Empty Activity.

Copy and paste the following content into your MainActivity.java, below the package declaration, and run the application.

Now open up LogCat to see when each of the lifecycle methods are being executed.

How to use logcat to see the android activity lifecycle

All that’s left for you to do now is to navigate in and out of the application, change screen orientation etc. to get a clear understanding of when each of the lifecycle methods in Android are being called!

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *