There are other ways to use fragments within your application You can create

There are other ways to use fragments within your

This preview shows page 49 - 55 out of 55 pages.

There are other ways to use fragments within your application. You can create reusable behavior components without a user interface. You can nest fragments within fragments.
Image of page 49

Subscribe to view the full document.

Behavior Fragments without a User Interface Fragments are not only for decoupling a user interface component from an Activity . You may also want to decouple application behaviors, such as background processing, into a reusable Fragment . Rather than providing the ID of a resource, you simply provide a unique string tag when adding or replacing a Fragment . Because you are not adding a particular View to your layout, the call to the onCreateView() method is never called. Just make sure to use the findFragmentByTag() to retrieve this behavior Fragment from your Activity .
Image of page 50
Exploring Nested Fragments A recent addition to Android 4.2 (API Level 17) is the ability to nest fragments within fragments. Nested fragments have also been added to the Android Support Library, making this API capability available all the way back to Android 1.6 (API Level 4).
Image of page 51

Subscribe to view the full document.

Exploring Nested Fragments (Cont’d) In order to add a Fragment within another Fragment , you must invoke the Fragment method getChildFragmentManager() , which returns a FragmentManager . Once you have the FragmentManager , you can start a FragmentTransaction by calling beginTransaction() and then invoking the add() method, including the Fragment to add and its layout, followed by the commit() method.
Image of page 52
Exploring Nested Fragments (Cont’d) Nested fragments bring many possibilities for creating dynamic and reusable nested components. Some examples include: Tabbed fragments within tabbed fragments Paging from one Fragment item/ Fragment detail screen to a next Fragment item/ Fragment detail screen with ViewPager Paging fragments with ViewPager within tabbed fragments Along with a host of many other use cases
Image of page 53

Subscribe to view the full document.

Chapter 9 Summary We have learned about the Fragment lifecycle and how it relates to the Activity lifecycle. We have learned how to define fragments. We are now able to manage Fragment modifications. We are now able to work with ListFragment and WebViewFragment . We should now be confident designing Fragment -based applications. We have learned that the Android Support Package is required for providing Fragment support to applications prior to API Level 11. We have introduced the concept of nested fragments.
Image of page 54