Anil Sai
Anil Sai
Android Developer

📡 Android Activity Lifecycle Guide

📡 Android Activity Lifecycle Guide

🧠 Mastering the Android Activity Lifecycle: A Modern Developer’s Guide

The Android Activity Lifecycle is fundamental to building responsive, memory-efficient, and user-friendly apps. Understanding how an Activity moves through its lifecycle is essential for everything from saving user data to avoiding memory leaks.

In this blog, we’ll break down the lifecycle stages with Kotlin examples and sprinkle in tips and tricks for making your apps shine in the real world.


🧩 What Is the Android Activity Lifecycle?

Every Activity in Android goes through a set of predefined states — from creation to destruction. Android calls lifecycle callback methods as the activity enters and exits these states, letting you handle UI setup, resource management, and state persistence appropriately.


🔄 Lifecycle Flow (Visual)

1
2
3
4
5
6
7
8
9
10
11
12
13
onCreate()
   ↓
onStart()
   ↓
onResume()
   ↓
-- (Activity is Running) --
   ↑
onPause()
   ↑
onStop()
   ↑
onDestroy()

🚀 Lifecycle Methods Explained (with Kotlin Code)

1️⃣ onCreate()

Called when the activity is first created. Set up views, bindings, ViewModel, and data sources here.

1
2
3
4
5
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    Log.d("Lifecycle", "onCreate")
}

💡 Tips:

  • Keep it lightweight. Avoid long-running operations.
  • Initialize only essential components.
  • Use ViewBinding/DataBinding for better maintainability.

2️⃣ onStart()

The activity is becoming visible.

1
2
3
4
override fun onStart() {
    super.onStart()
    Log.d("Lifecycle", "onStart")
}

💡 Tip: Ideal for UI updates that don’t need user interaction.


3️⃣ onResume()

The activity is now in the foreground and interactive.

1
2
3
4
override fun onResume() {
    super.onResume()
    Log.d("Lifecycle", "onResume")
}

💡 Use Cases:

  • Start animations
  • Resume camera or sensors
  • Check for app updates or messages

4️⃣ onPause()

Another activity is coming into the foreground. You should pause heavy resources here.

1
2
3
4
override fun onPause() {
    super.onPause()
    Log.d("Lifecycle", "onPause")
}

💡 Tips:

  • Pause video/audio playback.
  • Save UI state or data.
  • Don’t do heavy operations — it should be fast.

5️⃣ onStop()

The activity is no longer visible to the user.

1
2
3
4
override fun onStop() {
    super.onStop()
    Log.d("Lifecycle", "onStop")
}

💡 Tip: Release memory-heavy resources like background threads, sensors, or database connections.


6️⃣ onRestart()

Called when the activity moves from stopped to started again.

1
2
3
4
override fun onRestart() {
    super.onRestart()
    Log.d("Lifecycle", "onRestart")
}

💡 Use Case: Refresh data or UI if needed.


7️⃣ onDestroy()

The activity is finishing or being destroyed by the system.

1
2
3
4
override fun onDestroy() {
    super.onDestroy()
    Log.d("Lifecycle", "onDestroy")
}

💡 Tips:

  • Clean up any memory leaks.
  • Unregister listeners or observers.
  • If using coroutines, cancel active scopes.

✅ Full Activity Lifecycle Example (Kotlin)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class MainActivity : AppCompatActivity() {

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

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

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

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

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

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

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

💡 Pro Tips for Modern Android Development

✅ 1. Use ViewModel to Handle Configuration Changes

1
private val viewModel: MainViewModel by viewModels()

✅ 2. Leverage Lifecycle-Aware Components

1
2
3
4
5
class MyObserver : DefaultLifecycleObserver {
    override fun onStart(owner: LifecycleOwner) {
        Log.d("Observer", "Started")
    }
}

Attach it in your activity:

1
lifecycle.addObserver(MyObserver())

✅ 3. Avoid Memory Leaks

  • Never hold a reference to a Context or Activity in a static variable.
  • Always unregister callbacks and listeners in onPause() or onDestroy().

✅ 4. Use LifecycleScope for Coroutines

1
2
3
lifecycleScope.launch {
    // safe to run during lifecycle
}

🔚 Conclusion

Mastering the Activity lifecycle gives you fine-grained control over your app’s behavior, performance, and resource usage. Whether you’re handling UI updates, loading data, or managing memory, the lifecycle methods provide the right hooks at the right time.

comments powered by Disqus