Fragments
Fragments will likely become a staple of almost every real app you make. They are so useful; there are so many reasons to use them; and once you get used to them, they are so simple. There is almost no reason not to use them.
Fragments are reusable elements of an app just like any class, but as mentioned previously, they have special features, such as the ability to load their own view as well as having their very own lifecycle methods that make them perfect to achieve the goals that we've discussed in the Real-world apps section.
Let's dig a bit deeper into Fragments one feature at a time.
Fragments have a lifecycle too
We can set up and control Fragments very much like we do Activities, by overriding the appropriate lifecycle methods.
onCreate
In the onCreate
method, we can initialize variables and do almost all the things we would typically do in the Activity onCreate
method. The big exception to this is initializing our UI.
onCreateView
In this method, we will, as the name...