Fragments and Fragment Lifecycle
# CHAPTER 15
Fragments and Fragment Lifecycle
1. Introduction
In the early days of Android, developers built applications by creating 50 differentActivities and jumping between them using Intents. However, Activities are heavy, slow to load, and terrible for large screens (like iPads/Tablets) where you might want to show a List on the left and Details on the right simultaneously. To solve this, Google introduced Fragments. A Fragment is a lightweight, reusable chunk of User Interface that lives *inside* an Activity. In this chapter, we will master Fragments and the Fragment Lifecycle. We will explore the modern "Single-Activity Architecture," learn how to dynamically swap UI chunks using the FragmentManager, and analyze the unique Lifecycle of a Fragment.
2. Learning Objectives
By the end of this chapter, you will be able to:- Explain the architectural difference between an Activity and a Fragment.
- Understand the paradigm of Single-Activity Architecture (SAA).
- Create a reusable Fragment and its associated XML layout.
-
Dynamically add, replace, and remove Fragments utilizing the
FragmentManager.
-
Navigate the complexities of the Fragment Lifecycle (specifically
onCreateView).
3. Fragments vs Activities
- Activity: The heavyweight Window. An app must have at least one Activity. It is an independent entity registered in the Manifest.
- Fragment: A lightweight UI module. It is NOT registered in the Manifest. It cannot exist on its own; it must be hosted inside an Activity.
*Analogy:* The Activity is the television set. The Fragments are the different channels you swap out on the screen.
4. Single-Activity Architecture (SAA)
Today, Google strictly recommends Single-Activity Architecture. Instead of having aHomeActivity, SearchActivity, and ProfileActivity, modern apps use a single MainActivity. This Activity holds a "Bottom Navigation Bar" and a blank FrameLayout in the center of the screen. When the user taps the "Search" tab, the code simply injects the SearchFragment into that blank space. It is blazingly fast and incredibly smooth.
5. Creating a Fragment
Creating a Fragment requires two pieces, just like an Activity: the Kotlin class and the XML layout.The XML (fragment_home.xml):
The Kotlin (HomeFragment.kt):
Fragments do not use onCreate() to set their layout. They use a special lifecycle method called onCreateView.
6. The FragmentManager (Swapping Fragments)
How do we get theHomeFragment to actually appear inside our MainActivity?
First, the activity_main.xml must have a blank container ready to hold it:
Next, in MainActivity.kt, we use the supportFragmentManager to execute a Fragment Transaction.
7. The Fragment Lifecycle
Because a Fragment lives inside an Activity, its lifecycle is tied to the host Activity, but it has extra steps related to drawing its UI.-
onAttach(): The Fragment attaches to the Activity.
-
onCreate(): The Fragment is created (but not the UI).
-
onCreateView(): The XML is inflated and the UI is drawn!
-
onViewCreated(): The UI is ready. This is where you should put yourfindViewByIdlogic!
-
onResume(): The Fragment is actively running.
-
onDestroyView(): The UI is destroyed (but the Fragment logic stays alive in memory).
-
onDetach(): The Fragment is completely disconnected from the Activity.
8. Handling UI Logic in a Fragment (onViewCreated)
In an Activity, you use findViewById directly in onCreate. In a Fragment, you must wait until the view is fully created, and execute logic inside onViewCreated.
9. Common Mistakes
-
Crashing
requireActivity(): Fragments often need to ask their host Activity for information. If you callrequireActivity()while the Fragment is in the background or completely detached, the app will instantly crash with anIllegalStateException. Always ensure the Fragment isisAddedbefore communicating with the host Activity.
-
Memory Leaks with ViewBinding: If you use modern ViewBinding in a Fragment, you MUST set the binding variable to
nullinside theonDestroyView()callback. Unlike Activities, Fragments can outlive their visual views. If you hold onto the visual references, you cause a massive memory leak!
10. Best Practices
-
Never Communicate Fragment to Fragment directly: If
Fragment Aneeds to tellFragment Bsomething, do not try to link them directly.Fragment Ashould pass the data to the sharedViewModel(or the host Activity), andFragment Bshould observe that shared ViewModel. This keeps modules perfectly decoupled.
11. Exercises
-
1.
Create a blank Fragment class named
ProfileFragmentand its corresponding XML layout.
-
2.
Write the Kotlin transaction code in
MainActivityto.replace()the fragment container with your newProfileFragment.
12. Coding Challenges
Challenge: Modify theMainActivity. Add two Buttons at the top of the screen ("Home" and "Profile"). Below the buttons, place the fragmentContainer. Write click listeners for both buttons. When "Home" is clicked, trigger a transaction to load HomeFragment. When "Profile" is clicked, trigger a transaction to load ProfileFragment. You have just built a custom Navigation Tab system!
13. MCQ Quiz with Answers
In modern Android architecture, what is the explicit functional paradigm of Single-Activity Architecture (SAA)?
Within the Fragment Lifecycle, which specific callback function must be utilized to programmatically inflate the associated XML layout file (drawing the UI)?
14. Interview Questions
- Q: Explain the structural decoupling benefits of utilizing Fragments over monolithic Activities. How does this architecture specifically benefit tablet/foldable UI optimizations?
-
Q: Describe the execution sequence of the
FragmentManager. Why must a Fragment transition be finalized with the.commit()command?
-
Q: Contrast
onCreateView()withonViewCreated(). Why is executingfindViewByIdoperations strictly relegated toonViewCreated()rather thanonCreateView()?
15. FAQs
Q: I usedsupportFragmentManager.beginTransaction().add() instead of .replace(). Now my fragments are overlapping and I can see both of them! Why?
A: .add() literally stacks the new Fragment on top of the old one in the container (making it a transparent layer on top). .replace() completely destroys the old Fragment's view before injecting the new one. Always use .replace() for standard screen navigation!
16. Summary
In Chapter 15, we revolutionized our application structure by adopting modern Single-Activity Architecture (SAA). We isolated complex UI blocks into modular, lightweight Fragments, decoupling our presentation layer from heavyweight OS window constraints. We mastered the orchestration of these components, executing dynamic injection and extraction via theFragmentManager transaction API. Finally, we navigated the nuanced Fragment Lifecycle, ensuring strict architectural safety by isolating view binding logic to the onViewCreated execution phase.