Wed. Jan 15th, 2025

The Activity Lifecycle in Android defines the states and transitions an Activity goes through during its existence. Understanding the lifecycle is crucial for managing resources efficiently, handling state changes, and ensuring smooth user experiences.


Lifecycle Methods

Here are the key lifecycle methods in the order they are typically called:

1. onCreate()

  • Called when the Activity is created.
  • Initialize UI components, set up data bindings, or restore saved states.
  • Triggered once during the lifetime of an Activity.
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    // Initialize components here
}

2. onStart()

  • Called when the Activity becomes visible to the user.
  • The Activity is not yet interactive but visible on the screen.
override fun onStart() {
    super.onStart()
    // Perform actions like refreshing UI or data
}

3. onResume()

  • Called when the Activity starts interacting with the user.
  • The Activity enters the foreground state.
  • Ideal for starting animations, opening cameras, or other active tasks.
override fun onResume() {
    super.onResume()
    // Resume tasks, like restarting animations or playback
}

4. onPause()

  • Called when the Activity is partially obscured (e.g., a dialog appears).
  • Used to save data or release resources like animations, sensors, or music.
override fun onPause() {
    super.onPause()
    // Pause animations or release resources
}

5. onStop()

  • Called when the Activity is no longer visible to the user.
  • Free up resources and save persistent data.
  • It’s always followed by either onRestart() or onDestroy().
override fun onStop() {
    super.onStop()
    // Save application data or release resources
}

6. onRestart()

  • Called when the Activity is visible again after being stopped.
  • Often used to reinitialize resources or refresh data.
override fun onRestart() {
    super.onRestart()
    // Reinitialize components or update UI
}

7. onDestroy()

  • Called when the Activity is destroyed or the app process is terminated.
  • Cleanup resources like threads, databases, or listeners.
override fun onDestroy() {
    super.onDestroy()
    // Perform final cleanup
}

Diagram of Activity Lifecycle

Here’s a simplified representation of the lifecycle:

  1. Launch: onCreateonStartonResume
  2. Foreground (Interactive): Active in onResume
  3. Background or Paused: onPauseonStop
  4. Restart (if needed): onRestartonStartonResume
  5. Destroy: onStoponDestroy

Lifecycle Scenarios

App in Foreground

  • onCreateonStartonResume

App Goes to Background

  • onPauseonStop

App Returns to Foreground

  • onRestartonStartonResume

App is Closed

  • onPauseonStoponDestroy

Tips for Lifecycle Management

  1. Save Instance State: Use onSaveInstanceState to store data in case the Activity is recreated.
  2. Avoid Memory Leaks: Release heavy resources (e.g., listeners, sensors) in onPause or onStop.
  3. Optimize Performance: Only load UI-heavy resources in onResume.

Would you like a Kotlin code example demonstrating these lifecycle methods? 🚀

By Rajashekar

I’m (Rajashekar) a core Android developer with complimenting skills as a web developer from India. I cherish taking up complex problems and turning them into beautiful interfaces. My love for decrypting the logic and structure of coding keeps me pushing towards writing elegant and proficient code, whether it is Android, PHP, Flutter or any other platforms. You would find me involved in cuisines, reading, travelling during my leisure hours.

Leave a Reply

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