Fragment reality check
So, what does this Fragment
stuff really do for us? Our first Fragment
mini-app would have the same appearance and functionality had we not bothered with the Fragment
at all.
In fact, using the Fragment
class has made the whole thing more complicated! Why would we want to do this?
We know that a Fragment
instance or fragments can be added to the layout of an Activity
.
We know that a Fragment
not only contains its own layout (view) but also its very own code (controller), which, although hosted by an Activity
, the Fragment
instance is virtually independent.
Our quick app only showed one Fragment
instance in action, but we could have an Activity
that hosts two or more fragments. We then effectively have two almost independent controllers displayed on a single screen.
What is most useful about this, however, is that when the Activity
starts, we can detect attributes of the device our app is running on – perhaps phone or tablet; portrait or...