AnDevCon talk on Fragments

I gave a talk on Android Fragments on May 29, 2014 at AnDevCon in Boston. The talk was well attended and well received. You can download the slide deck from the presentation if you like.

Life used to be simple. We had Activities and they were good. You could define all of your app's UI in a layout and animate it in an Activity. Then Apple brought out the iPad and Google answered with Honeycomb. Things got complex. Google realized that tablets would drive the need for developers to reuse parts of their UI, and the answer was the Fragment class. Fragments enable developers to use bits of UI along with the associated business logic in various parts of their app or even in multiple apps.

The Slide deck has a matching github repository that has several really useful example projects in it. The easiest way to build the examples is with Android Studio and gradle.

fragment-logged

This example app uses native fragments to demonstrate several concepts:

  1. Dynamic vs Static Fragments: Static fragments are created with the <fragment> tag in a layout. Dynamic fragments are added at run time to a <frame> tag.
  2. Using a Bundle to store Activity state information throughout the Activity's lifecycle.
  3. The interrelationship of the Activity lifecycle with the Fragment lifecycle. There is a really useful set of classes (CallLogger, LoggedActivity and LoggedFragment) that enable you to log calls from lifecycle events without adding any code to your Activities and Fragments themselves.
  4. How the Back button and configuration changes (screen rotation) affect the Activity and Fragment lifecycles.

fragment-comms

This example app demonstrates five styles of communicating data between Activities and Fragments.

Fragment Communication Chart

fragment-comms-support

This project is just like the fragment-comms project except that it uses the support library.

hall-of-mirrors

The hall-of-mirrors example demonstrates that Fragments can be nested within other Fragments.

comments powered by Disqus