android-kotlin
/

Android Fragments – Building Modular and Reusable UI

Last Sync: Today

On this page

5
0%
5 min read
Remaining
5 minleft

Click any section to jump — progress syncs automatically

android-kotlin

Android Fragments – Building Modular and Reusable UI

What is a Fragment?

A Fragment is a self-contained UI component that must reside within a 'Host' Activity. Think of a Fragment as a 'Sub-Activity' that has its own lifecycle and receives its own input events. For an Engineering Manager, Fragments are the key to Multi-Pane Layouts—where a single Activity might show a list on the left and details on the right using two separate Fragments.

  1. The Fragment Lifecycle

The Fragment lifecycle is closely tied to its host Activity, but it has additional states specifically for managing its 'View'. This separation is crucial for a Lead Architect to understand, as a Fragment can exist even when its UI (View) has been destroyed to save memory.

  • onAttach(): Called when the fragment is first associated with its activity.
  • onCreateView(): The most important method—this is where you inflate the XML layout or initialize the Compose UI for the fragment.
  • onViewCreated(): Called immediately after onCreateView. Best place to set up listeners and ViewModels.
  • onDestroyView(): Called when the fragment's UI is being removed. This is where you clean up View references.
  • onDetach(): The final step—the fragment is no longer associated with the activity.

  1. FragmentManager and Transactions

To add, remove, or replace Fragments at runtime, you use the FragmentManager to start a FragmentTransaction. These operations are asynchronous and can be added to a 'Back Stack', allowing the user to navigate 'back' through Fragment changes just like they would through Activities.

KOTLINRead-only
1
// Swapping to an AI Preview Fragment in your native Runner
val fragment = AIPreviewFragment.newInstance(templateId = "revo_01")

supportFragmentManager.beginTransaction()
    .replace(R.id.fragment_container, fragment)
    .addToBackStack(null) // Allows the 'Back' button to reverse this
    .commit()

  1. Communication: Fragment to Activity

Fragments should be independent. To communicate with their host, we avoid direct references. In 2026, the standard is to use a Shared ViewModel. Both the Activity and its Fragments observe the same ViewModel instance, ensuring that a change in the AI data layer is reflected instantly across the entire UI.

Fragment Role Comparison

ScenarioFragment UsageFlutter Equivalent
UI ModularityFragment (Modular View)Custom Widget
NavigationFragmentTransactionNavigator.push()
Screen PortionPartial screen (Pane)Expanded / Flexible Widget
Tab ContentViewPager2 + FragmentsTabBarView
DialogsDialogFragmentshowDialog()

Test Your Knowledge

Q1
of 3

Which lifecycle method is specifically responsible for inflating the Fragment's UI layout?

A
onCreate()
B
onStart()
C
onCreateView()
D
onAttach()
Q2
of 3

What object is used to perform actions like adding or replacing fragments?

A
FragmentIntent
B
FragmentTransaction
C
ActivityContext
D
ViewManager
Q3
of 3

What is the recommended way for an Activity and its Fragments to share data in 2026?

A
Passing a direct reference of the Activity to the Fragment
B
Using Static Global variables
C
Using a Shared ViewModel
D
Writing data to a local file

Frequently Asked Questions

Why use a Fragment instead of just another Activity?

Fragments are more lightweight and flexible. They are ideal for creating adaptive UIs (like tablet vs. phone) and allow you to keep the user within the same Activity context while changing the content, which is faster and uses less memory.

What is the 'Back Stack'?

The Fragment Back Stack is a list of FragmentTransactions that the system saves. When the user presses the 'Back' button, the system pops the last transaction off the stack, effectively undoing the last Fragment change.

How do I pass data to a Fragment?

The professional way is using 'Arguments'. You create a Bundle, put your data in it, and call 'setArguments()' before the fragment is attached. This ensures the data survives if the OS kills and recreates the fragment.

Previous

android activity

Next

android intents

Related Content

Need help?

Explore our comprehensive docs or start a chat with our tech experts.