Android Activity & Lifecycle

What is an Activity in Android?

An Activity in Android is a single screen in an app. For example, when you open a messaging app, the screen showing the list of messages is one Activity. When you tap to view a specific conversation, it opens a new Activity. Think of Activities as different pages or screens in an app. They let users interact with the app’s features, like clicking buttons, typing text, or viewing information.


The Android Activity Lifecycle

Activities in an Android app don’t stay the same forever. Android manages them through a process called the Activity Lifecycle. This lifecycle ensures that Activities are created, paused, stopped, and destroyed as needed. By managing Activities, the Android system saves memory and runs apps smoothly.

Here are the key stages of the Android Activity lifecycle:

1. onCreate() – Activity is Created

When you open an app for the first time, the onCreate() method runs. This stage is where the app sets everything up. The screen layout is loaded, and the app gets ready for use. It’s like waking up in the morning and getting dressed for the day.

2. onStart() – Activity is Visible

After the Activity is created, it becomes visible to the user. The onStart() method makes the screen appear, but it’s not fully interactive yet. Think of it as being ready to start a task, but not doing anything just yet.

3. onResume() – Activity is Active

Now, the Activity is fully active. This is when you can interact with it. You can tap buttons, scroll, and type. The onResume() method brings the app to the foreground, allowing user interaction. It’s like sitting down to work and starting your tasks.

4. onPause() – Activity is Paused

When you switch to another app, the onPause() method runs. The Activity is still there but not in focus. It’s like stepping away from your desk for a moment. The app can pause any tasks but remains in memory for quick access.

5. onStop() – Activity is Hidden

If you open a different app or go to the home screen, the Activity is no longer visible. The onStop() method runs, and the Activity is now hidden. It’s still running in the background but not in use. Think of this as walking away from your desk, leaving your work behind.

6. onRestart() – Activity is Restarted

If you come back to the app after it has been stopped, the onRestart() method prepares the Activity to resume. It’s like returning to your desk after a break to continue working.

7. onDestroy() – Activity is Destroyed

When you close the app or Android needs to free up memory, the onDestroy() method is called. This method shuts everything down and clears resources. It’s like finishing your work for the day and shutting down your computer.


A Real-Life Example of the Activity Lifecycle

Let’s say you’re using a music app:

  • onCreate(): You open the app, and the music library appears.
  • onStart(): The screen becomes visible, but you haven’t chosen a song yet.
  • onResume(): You tap on a song and start playing it.
  • onPause(): You get a phone call, so the music pauses while you’re on the call.
  • onStop(): You go back to your home screen after the call, and the music stops.
  • onRestart(): You return to the app, and the song starts playing again.
  • onDestroy(): You close the app when you’re done listening, and the app shuts down.

Why Does the Activity Lifecycle Matter?

The Android system manages many apps at once. The Activity lifecycle ensures that apps don’t use too much memory or run when they’re not needed. It also helps apps save data, so users can continue where they left off. Developers use this lifecycle to make sure their apps work smoothly without crashing.

For more details, visit the official Android Developers page on Activity Lifecycle to understand best practices.


Our Blogs about other android topics

https://androidgurukula.com/understanding-android-fragment-and-its-lifecycle-a-simple-guide/