Fragment reality check

So, what does this Fragment stuff really do for us? Our first Fragment mini app would have exactly the same appearance and functionality had we not bothered with Fragment at all. In fact, using Fragment has made the whole thing more complicated! Why would we want to do this?

We kind of know the answer to this already, but it just isn't especially clear based on what we have seen so far. We know that Fragment 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 Activity, the Fragment is virtually independent.

Our quick mini app only showed one Fragment in action, but we could have an Activity that hosts two or more fragments. We can then effectively have two almost-independent controllers displayed on a single screen. This sounds like it could be useful.

What is most useful about this, however, is that when the Activity starts, we can detect attributes of the device that our app is running on, perhaps a phone or tablet or in the portrait or landscape orientation. We can then use this information to decide to display either just one or two of our fragments simultaneously.

This not only helps us achieve the kind of functionality discussed in the Real-world apps section at the start of the chapter, but it also allows us to do so using the exact same Fragment code for both possible scenarios! This really is the essence of fragments. We can create a complete app by pairing up both functionality (controller) and appearance (view) into a bunch of fragments that we can reuse in different ways, almost without care.

It is, of course, possible to foresee a few stumbling blocks, so take a look at these FAQs.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.54.199