Android Activity Lifecycle with Kotlin

Android Activity Lifecycle with Kotlin

Introduction

Understanding the Android Activity Lifecycle is crucial for building efficient and user-friendly applications. This lifecycle defines how an Activity behaves as it transitions through different states. In this article, we will explore all lifecycle stages with Kotlin examples, best practices, and handling configuration changes like screen rotations.

What is the Activity Lifecycle?

An Activity in Android goes through different states based on user interactions, system events, or device configuration changes. The lifecycle consists of several callback methods, which Android invokes when the state changes.

Each lifecycle method serves a specific purpose, and understanding them can help you manage UI updates, save data efficiently, and optimize resource usage.

Below is the complete Activity lifecycle flow:

Now, let's dive into each lifecycle method with Kotlin examples.

1. Created (onCreate())

This method is called when the Activity is first created.

It is used for initial setup, such as:

  • Setting up the UI components.

  • Initializing variables.

  • Restoring saved instance state.

Called only once in the Activity lifecycle.

Example:

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        Log.d("Lifecycle", "onCreate called")
    }
}

2. Started (onStart())

Called when the Activity becomes visible to the user but is not yet interactive.

Can be called multiple times if the Activity is restarted.

Use it to refresh UI elements, register listeners, or update data.

Example:

override fun onStart() {
    super.onStart()
    Log.d("Lifecycle", "onStart called")
}

3. Resumed (onResume())

Called when the Activity comes to the foreground and becomes interactive.

This is where user input and UI updates happen.

Use this method to resume animations, start camera previews, or connect to a live data source

Example:

override fun onResume() {
    super.onResume()
    Log.d("Lifecycle", "onResume called")
}

4. Paused (onPause())

Called when the Activity is partially visible (e.g., a dialog appears).

The Activity is still alive, but user interaction is paused.

Use it to save unsaved changes, pause animations, or release resources like camera or microphone.

This method executes quickly because onStop() might follow soon

Example:

override fun onPause() {
    super.onPause()
    Log.d("Lifecycle", "onPause called")
}

5. Stopped (onStop())

Called when the Activity is no longer visible (e.g., the user switches to another app).

The Activity is still in memory but is in the background.

Use this method to release heavy resources like database connections or unregister listeners.

If the user navigates back, onRestart() is called instead of onCreate().

Example:

override fun onStop() {
    super.onStop()
    Log.d("Lifecycle", "onStop called")
}

6. Restarted (onRestart())

Called when the Activity is coming back to the foreground after being stopped.

Use this method to refresh UI or reinitialize components or reload data.

Example:

override fun onRestart() {
    super.onRestart()
    Log.d("Lifecycle", "onRestart called")
}

7. Destroyed (onDestroy())

Called when the Activity is permanently removed from memory.

Happens when:

  • The user exits the Activity (back button or system kills it).

  • The app is terminated.

Use this method to clean up memory, unregister listeners, and close database connections.

Example:

override fun onDestroy() {
    super.onDestroy()
    Log.d("Lifecycle", "onDestroy called")
}

Handling Configuration Changes (e.g., Screen Rotation)

By default, when the device rotates, Android destroys and recreates the Activity. This can lead to data loss if not handled properly.

Solution: Use onSaveInstanceState() to save UI state.

override fun onSaveInstanceState(outState: Bundle) {
    super.onSaveInstanceState(outState)
    outState.putString("message", "Hello, World!")
}

override fun onRestoreInstanceState(savedInstanceState: Bundle) {
    super.onRestoreInstanceState(savedInstanceState)
    val message = savedInstanceState.getString("message")
    Log.d("Lifecycle", "Restored message: $message")
}

Alternatively, you can use ViewModel to retain data across configuration changes.

Best Practices for Activity Lifecycle

  • Use onCreate() for initializing UI components and restoring saved data.

  • Use onStart() and onStop() for UI updates and event listeners.

  • Use onResume() and onPause() for real-time interactions like camera or GPS.

  • Use onDestroy() to release resources and avoid memory leaks.

  • Handle configuration changes using ViewModel or onSaveInstanceState().

Final Thoughts

Mastering the Android Activity Lifecycle is key to building robust applications. By using the right lifecycle methods at the right time, you can enhance performance, optimize resource usage, and provide a seamless user experience.

Happy Coding!