Activity & Fragment Lifecycle (Internals)
📖 Concept
The Activity lifecycle is the most fundamental concept in Android development. At the senior level, you need to understand not just the callbacks, but the internal state machine that drives them.
Activity Lifecycle States & Transitions:
[DOES NOT EXIST]
↓ onCreate()
[CREATED] ──── onStart() ────→ [STARTED]
↓ onResume()
[RESUMED] (foreground, interactive)
↓ onPause()
[STARTED] (visible but not interactive)
↓ onStop()
[CREATED] (not visible)
↓ onDestroy()
[DOES NOT EXIST]
Internal working:
- ActivityManagerService (AMS) in system_server process manages lifecycle transitions via Binder IPC
- The TransactionExecutor in the app process receives lifecycle transactions and executes them in order
- Each state transition is a ClientTransaction containing ActivityLifecycleItem callbacks
- Config changes (rotation) trigger: onPause → onStop → onDestroy → onCreate → onStart → onResume
Fragment Lifecycle (additional complexity):
- Fragments have their own lifecycle PLUS a view lifecycle (getViewLifecycleOwner)
- Fragment lifecycle is managed by FragmentManager's state machine
- Fragment view can be destroyed while Fragment instance lives (e.g., back stack)
Critical edge cases that cause production bugs:
- onSaveInstanceState timing — Called between onPause and onStop (pre-API 28) or after onStop (API 28+)
- Fragment transaction after onSaveInstanceState — IllegalStateException crash
- Activity recreation — ViewModel survives, but Activity/Fragment instances don't
- Multi-window mode — Both Activities can be in STARTED state, but only one is RESUMED
💻 Code Example
1// Lifecycle-aware component that handles edge cases23// BAD: Leaks and crashes ❌4class LeakyActivity : AppCompatActivity() {5 private var networkCallback: NetworkCallback? = null67 override fun onCreate(savedInstanceState: Bundle?) {8 super.onCreate(savedInstanceState)9 // This callback outlives the Activity on config change!10 networkCallback = object : NetworkCallback() {11 override fun onDataReceived(data: String) {12 textView.text = data // Crash: textView may be null after destroy13 }14 }15 NetworkManager.register(networkCallback!!)16 }17 // Missing: unregister in onDestroy → memory leak + crash18}1920// GOOD: Lifecycle-aware, no leaks ✅21class SafeActivity : AppCompatActivity() {22 override fun onCreate(savedInstanceState: Bundle?) {23 super.onCreate(savedInstanceState)2425 lifecycle.addObserver(object : DefaultLifecycleObserver {26 override fun onStart(owner: LifecycleOwner) {27 NetworkManager.register(callback)28 }29 override fun onStop(owner: LifecycleOwner) {30 NetworkManager.unregister(callback)31 }32 })33 }34}3536// Fragment View Lifecycle — common source of bugs37class MyFragment : Fragment(R.layout.my_fragment) {38 private var _binding: MyFragmentBinding? = null39 private val binding get() = _binding!!4041 override fun onViewCreated(view: View, savedInstanceState: Bundle?) {42 super.onViewCreated(view, savedInstanceState)43 _binding = MyFragmentBinding.bind(view)4445 // Use viewLifecycleOwner for UI observations, NOT this (fragment)46 viewLifecycleOwner.lifecycleScope.launch {47 viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {48 viewModel.uiState.collect { state -> binding.update(state) }49 }50 }51 }5253 override fun onDestroyView() {54 super.onDestroyView()55 _binding = null // Prevent memory leak — view is destroyed but fragment lives56 }57}
🏋️ Practice Exercise
Practice:
- Draw the complete Activity lifecycle with all callbacks from memory
- What's the difference between onStop and onDestroy? When is onDestroy NOT called?
- Explain the Fragment view lifecycle vs Fragment lifecycle — why are they different?
- What happens to the lifecycle when a Dialog appears over your Activity?
- Write a lifecycle-aware component using DefaultLifecycleObserver
- Explain what happens during a configuration change — which objects survive?
- What is the difference between finish(), moveTaskToBack(), and System.exit()?
⚠️ Common Mistakes
Observing LiveData/Flow with Fragment's lifecycle instead of viewLifecycleOwner — causes duplicate observers after back stack navigation
Not nullifying view binding in onDestroyView — Fragment instance outlives its view on the back stack, causing memory leak
Committing FragmentTransactions after onSaveInstanceState — causes IllegalStateException, use commitAllowingStateLoss only as last resort
Assuming onDestroy is always called — it's NOT called when the system kills the process
Doing heavy work in onCreate — blocks the main thread, causes ANR if > 5 seconds
💼 Interview Questions
🎤 Mock Interview
Mock interview is powered by AI for Activity & Fragment Lifecycle (Internals). Login to unlock this feature.