Static vs. Dynamic Fragments
Sometimes, you will have a fragment that should be around as long as your activity is around. For that, you can use “static fragments”, where you use an element in a layout resource to specify where the fragment should go and how big it should be.
More often, though, your fragments will come and go, based on user input:
- You start by showing a list of stuff
- The user clicks on an item in the list, and you show details about the item that the user clicked on
- The user clicks an “edit” option in the toolbar, and so you show an edit form to allow the user to modify the item
- The user clicks BACK to return to the details, then BACK again to return to the list
In all of these cases, in a fragment-based UI, you will use dynamic fragments.
Roughly speaking, there are two ways of employing dynamic fragments:
- Manually, using
FragmentManager
andFragmentTransaction
classes, as we will explore in this chapter - Using the Jetpack Navigation component, which we will see in the next chapter
Prev Table of Contents Next
This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.