Quick guide to Android activity lifecycle

Quick guide to Android activity lifecycle

A brief and quick guide on lifecycle of an activity

Disclaimer :

This article is intended for those already familiar with the Android lifecycle who are looking for a quick summary to prepare for an interview or as a refresher.

The Android lifecycle defines the states an app goes through from launch to termination, handling events like screen rotations and backgrounding.

Android Lifecycle:

When you launch an app or navigate to another activity, the activity goes through this hierarchical lifecycle.

1 . onCreate()

It is called when the activity is created for the first time. It takes care of initializing essential components like views, binding data, or setting up resources.

Example: Inflate the UI layout using setContentView().

2 . onStart()

It is called when the activity becomes visible to the user, but the user can’t interact with the activity yet. It takes care of performing tasks like starting animations or initializing UI updates.

3 . onResume()

It is called when the activity starts interacting with the user

4 . onPause()

It is called when the activity goes partially out of view, like when another activity opens on top. (e.g. when a dialog box appears over an activity)

5 . onStop()

It is called when the activity is no longer visible. (e.g. navigating to another activity). This is when heavy resources are released, app data can be saved, and ongoing tasks can be stopped.

6 . onRestart()

It is called when the activity restarts after being stopped. (e.g., when the user navigates back to the previous activity from the current one)

7 . onDestroy()

It is called when the activity is destroyed either by the user (e.g., back button) or the system (e.g., low memory).

Quick Tip: Understand the diagram and understand the hierarchy of how the functions are called

If you want to dig deeper or trying to understand more here is an awesome video about the Android lifecycle: Android Lifecycle

Some interview questions that might be asked about this concept:

  1. name all the Android lifecycle and their functionality

  2. activity lifecycle behavior under different scenarios (e.g., what will be the lifecycle when you navigate to another activity)

That’s all from me 😁. If I am lacking somewhere or this article needs some correction just comment down. Bye!!!👋