BCA / B.Tech 9 min read

Understanding Activities and their Lifecycle

Understanding Activities and their Lifecycle

What is an Activity?

In Android, an Activity is the part of the application that the user can see and interact with. In simple terms, it represents a single screen of an app.

Whenever you use an Android app, you are interacting with its different activities.

  • Examples:
  • A WhatsApp chat screen is an activity.
  • The Instagram feed screen is an activity.
  • The phone's settings screen is an activity.

Each activity has its own User Interface (UI), which is defined in an XML layout file, and its behavior (how it works) is written in a Java or Kotlin class.


What is the Activity Lifecycle?

The Activity Lifecycle is the sequence of all the stages that an activity goes through from the moment it is launched until it is destroyed. The Android OS manages these stages, but it's important for us to understand them so we can take the right actions at the right time.

Think of it like a person's life: being born (created), becoming active (resumed), and finally, ending (destroyed).


Key Methods of the Activity Lifecycle

Android provides us with special methods (called callback methods) that are automatically called when the state of an activity changes.

  1. onCreate()
    • This method is called when the activity is first created.
    • This is the most important method. It is where you set up your UI (using `setContentView()`), initialize buttons, and perform other one-time setups.
  2. onStart()
    • This is called when the activity is about to become visible to the user.
  3. onResume()
    • This is called when the activity comes into the foreground and the user can interact with it. An activity stays in this state for the longest time.
  4. onPause()
    • This is called when the activity is about to lose focus (e.g., a popup appears or the user presses the home button).
    • In this method, you should save any data that needs to persist even if the user leaves the app.
  5. onStop()
    • This is called when the activity is no longer visible to the user at all.
  6. onRestart()
    • This method is called when the activity is about to become visible to the user again after being in the `onStop()` state.
  7. onDestroy()
    • This is the final method of the lifecycle. It is called when the activity is being permanently removed from memory.
    • All resources should be released here to prevent memory leaks.

A Practical Example of the Lifecycle

Imagine you have opened a music player app:

  1. You open the app: `onCreate()` -> `onStart()` -> `onResume()` are called. The app is now running.
  2. A friend calls you: Your music app goes into the background. `onPause()` -> `onStop()` are called.
  3. You end the call and return to the app: `onRestart()` -> `onStart()` -> `onResume()` are called. The app resumes from where you left off.
  4. You press the back button and close the app: `onPause()` -> `onStop()` -> `onDestroy()` are called. The activity is removed from memory.