Fragments
Fragments will likely become a staple of almost every 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/layout as well as their very own lifecycle methods, which make them perfect for achieving the goals we discussed in the Real-world apps section and having different layouts and code for different devices (like the weather app we looked at).
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 theonCreate
method, we can initialize variables and do almost all the things we would typically have done in the...