Fragment Lifecycle
ANDROID › Lifecycle
Drills the fragment vs view lifecycle split, viewLifecycleOwner, leak-safe observation, and FragmentManager back stack.
Fragment lifecycle is a perennial interview filter because the fragment and its view have two separate lifecycles, and conflating them causes real production leaks and crashes. Interviewers probe whether you know the callback order, why you observe LiveData/Flow with viewLifecycleOwner, and how FragmentManager and the back stack cap a fragment's state. Expect to explain the activity-fragment mismatch and what happens to the view across configuration changes and back stack navigation.
What this covers
- The five Lifecycle.State values and the full upward/downward callback order
- Why a fragment has TWO lifecycles: the fragment itself and its view (onCreateView..onDestroyView)
- Using viewLifecycleOwner vs the fragment as LifecycleOwner to avoid leaking observers
- How FragmentManager, setMaxLifecycle, and the back stack constrain a fragment's max state
- The fragment/activity lifecycle mismatch and clearing view references in onDestroyView
- Back stack vs replace/remove behaviour and why fragment instances must not be reused
Study this topic
- Fragment Lifecycle explained: the guided lesson
- 14 practice quiz questions
- 9 revision flashcards
- Fragment Lifecycle interview questions and answers