Android Activity Lifecycle

Android Guy
5 min readNov 14, 2018

--

This is the 2nd episode of my Android Series, in which you will be learning…

  1. Activity Lifecycle

Let’s get start!

What is Activity?
An activity is a single, focused thing that the user can do. Almost all activities interact with the user, so the Activity class takes care of creating a window for you in which you can place your UI (accessed Aug 9, 2018)

The technical explanation of Activity is that Activity is a contract between developers and Android system. Each state of its lifecycle is a system callback.

What is this Activity Lifecycle?
Activity Lifecycles are just like people’s lifecycles in the way that we were born, we live, and then we die. Sometimes, during our lives, we take short and long holidays. While some people live their long and healthy live, some people suffer from injuries and illness. Some even die from unexpected events such as having accidents.

As user navigates through and interacts with our Application, Activities will undergo many different states of their lives. Activities are created, live, and then are destroyed. While some Activities are temporarily paused, some are completely stoped and possibly destroyed. While some Activities run normally, some may crash unexpectedly and restart.

Let’s examine each state of the Lifecycle from the diagram above.

onCreate()
An Activity enters the Created state in which it is created once per its lifetime per instance. This is the best time to do any operations that should be performed once such as binding Views or associating Activity with ViewModel. Any Lifecycle-Awareness components that are tied up to the Activity will as well be notified of ON_CREATE event.

If finish() is invoked, onDestroy() will be called right after and the rest of Activity Lifecycle such as onStart(), onResume(), onPause() and onStop() will be skipped. At this stage, the Activity is not yet visible.

onStart()
Activity then enters its Started state in which it becomes visible, however, not yet in the foreground so user may not be able to see it. Any Lifecycle-Awareness components that are tied up to the Activity will receive ON_START event.

This stage is the best time to initialize any resources that are needed for the Activity, start any services, and to register any receivers such as BroadcastReceiver. Both onCreate() and onStart() should finish quickly otherwise user will experience some lag or delay before they can start seeing and interacting with the Activity.

onResume()
Activity then enters the Resumed state in which it has user focus and is in foreground. Any Lifecycle-Awareness components that are tied up to the Activity will also receive ON_RESUME event. The Activity remains in this stage until there is an interruption such as receiving a phone call, turning off the screen, and navigating away to other Applications or Activities.

This stage is the best time to run any components that need to be in the foreground to run such as a camera preview. However, such component is recommended to be implemented in an independent-lifecycle-aware class.

onPause()
As soon as there is such an interruption as mentioned above, Activity exits the foreground and enters the Paused state. Any Lifecycle-Awareness components that attach to the Activity will receive ON_PAUSE event which indicates that user is leaving the Activity.

At this stage, user can still see the Activity such as when it is inside a multi-windows mode where the user can still see the entire Activity or it is obscured by a semi-transparent UI component such as Dialog shown below.

Left -> 2 Activities inside Multi-Windows. Right -> Activity is obscured by a semi-transparent UI.

Activity remains in this Paused state as long as the Activity is still visible. It can either change its state back to the Resumed state if user chooses to resume the Activity or changes its state to Stopped state if the entire Activity becomes completely invisible.

onStop()
Activity then enters the Stopped state in which it has become completely invisible and is now in the background. Any Lifecycle-Awareness components that are tied up to the Activity will receive ON_STOP event.

At this stage, the Activity is still alive. Its object and state-data are retained in memory, however, those memory can be reclaimed anytime by the system if it is needed elsewhere so this is good time to save the state-data.

Please note that if the system destroys the Activity for any reasons, View’s states are automatically maintained so there is no need to manually save and restore them.

onDestroy()
This callback is invoked just before Activity will actually get destroyed. The Activity moves toward the destroyed state and any Lifecycle-Awareness components that are tied up to the Activity will receive ON_DESTROY event.

An Activity gets destroyed for 2 reasons.
1. The Activity is finishing, for example, finish() is invoked.
2. The configuration changes or entering the multi-windows mode.

To distinguish between the two, please use the isFinishing() method. This stage is the last chance to clean up memory and release any resources before the Activity is destroyed.

Last but not lease, the above diagram is an oversimplification of Activity Lifecycle yet it is very crucial to understand what each of the callback does and when it does happen. Below is the complete diagram of Activity Lifecycle.

Thank you and Credit to steve@staticfree.info (https://github.com/xxv/android-lifecycle)

--

--

Android Guy

Lead Software Engineer @ Property Guru, An Android Expert and An Photography Enthusiast.