Interview QuestionPractical QuestionFollow-up Questions

Fragment Lifecycle

skydovesJaewoong Eum (skydoves)||10 min read

Fragment Lifecycle

Fragments have their own lifecycle that is closely tied to, but distinct from, the lifecycle of the host Activity. A Fragment transitions through creation, view creation, visibility, interactivity, and destruction stages, each with dedicated callbacks. The separation between the Fragment's lifecycle and its View's lifecycle is one of the most frequently misunderstood aspects of Android development. By the end of this lesson, you will be able to:

  • Trace the Fragment lifecycle callbacks from onAttach() through onDetach().
  • Distinguish between the Fragment lifecycle and the Fragment View lifecycle.
  • Explain why view references must be cleared in onDestroyView().
  • Identify the correct callbacks for initializing data versus initializing UI.
  • Describe how viewLifecycleOwner scopes observations to the View lifecycle.

Attachment and Creation

When a Fragment is added to a FragmentManager, the first callback is onAttach(). At this point the Fragment has a reference to its host Activity through requireActivity() and can access the Activity's context. This is the earliest point where you can interact with the host.

onCreate() follows and is where you initialize data that should persist across view recreation. This includes setting up ViewModels, processing arguments from the Fragment's arguments Bundle, and restoring saved state. No view operations should happen here because the view does not exist yet.

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    val userId = requireArguments().getString("user_id")
    viewModel.loadUser(userId)
}

The distinction between Fragment creation and view creation matters because a Fragment can outlive its view. When a Fragment is placed on the back stack, the system destroys its view hierarchy to free memory but keeps the Fragment instance alive. The Fragment's ViewModel also survives this transition, maintaining all in memory data. When the user returns, onCreateView() is called again on the same Fragment instance, and the ViewModel provides the data needed to repopulate the new view.

View Creation and Interaction

onCreateView() inflates and returns the Fragment's layout. onViewCreated() fires immediately after and is the standard place to bind views, set up RecyclerView adapters, observe LiveData or Flow from the ViewModel, and attach click listeners.

override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
): View {
    return inflater.inflate(R.layout.fragment_user, container, false)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    viewLifecycleOwner.lifecycleScope.launch {
        viewModel.userState.collect { state ->
            bindUserData(state)
        }
    }
}

Using viewLifecycleOwner instead of this (the Fragment itself) for lifecycle scoped observations is important. The Fragment's lifecycle extends beyond its view. If you collect a Flow using the Fragment as the lifecycle owner, the collection continues even after the view is destroyed (when the Fragment is on the back stack). This causes crashes when the collector tries to update views that no longer exist. viewLifecycleOwner is scoped to the view lifecycle and automatically cancels when onDestroyView() runs.

This interview continues for subscribers

Subscribe to Dove Letter for full access to exclusive interviews about Android and Kotlin development.

Become a Sponsor