0Pricing
Android Academy · Lesson

Fragments

Build modular UIs with Fragments. Learn the fragment lifecycle, ViewBinding in fragments, argument passing, back stack management, and fragment-activity communication.

What Are Fragments?

A Fragment is a reusable UI module that lives inside an Activity. Think of it as a sub-Activity with its own layout, lifecycle, and logic.

Why use Fragments?

  • Reuse UI across multiple Activities
  • Handle different layouts for phone vs tablet
  • Build multi-screen flows without multiple Activities
  • Work naturally with Navigation Component

Fragment Lifecycle

Fragments have their own lifecycle that runs alongside the Activity lifecycle. Key callbacks:

  • onAttach — fragment attached to Activity
  • onCreate — fragment created (no view yet)
  • onCreateView — inflate the layout
  • onViewCreated — view ready, set up UI here
  • onDestroyView — view destroyed (clear view references)
  • onDetach — fragment detached

All lessons in this course

  1. Project Structure & Manifest
  2. Activities & Lifecycle
  3. Layouts & Views
  4. Handling User Input
  5. Intents & Navigation
  6. Fragments
  7. Permissions & Runtime Requests
← Back to Android Academy