Wednesday, May 16, 2012

20. Fragments

20.1. Fragments Overview

Fragment components allow you to organize your application code so that it is easier to support different sized devices.
Fragments are components with their own lifecycle and their own user interface. They can be defined via layout files or via coding.
Fragments always run in the context of an Activity. If an Activity is stopped its Fragments will also be stopped; if an Activity is destroyed its Fragments will also get destroyed.
If a Fragment component is defined in an XML layout file, the android:name attribute points to the Fragments class.
The base class for Fragments is android.app.Fragment. For special purposes you can also use more special classes, like ListFragment or DialogFragment.
The onCreateView() method is called by Android once the Fragment should create its user interface. Here you can inflate an layout. The onStart() method is called once the Fragment gets visible.
Fragments can be dynamically added and removed from an Activity via Fragment transactions. This will add the action to the history stack of the Activity, i.e. this will allow to revert the Fragment changes in the Activity via the back button.

20.2.  When to use Fragments

Fragments make it easy to re-use components in different layouts, e.g. you can build single-pane layouts for handsets (phones) and multi-pane layouts for tablets.
This is not limited to tablets; for example you can use Fragments also to support different layout for landscape and portrait orientation. But as tablets offer significantly more space you typically include more views into the layout and Fragments makes that easier.
The typical example is a list of items in an activity. On a tablet you see the details immediately on the same screen on the right hand side if you click on item. On a handset you jump to a new detail screen. The following discussion will assume that you have two Fragments (main and detail) but you can also have more. We will also have one main activity and one detailed activity. On a tablet the main activity contains both Fragments in its layout, on a handheld it only contains the main fragment.
To check for an fragment you can use the FragmentManager.
    
DetailFragment fragment = (DetailFragment) getFragmentManager().
  findFragmentById(R.id.detail_frag);
if (fragment==null || ! fragment.isInLayout()) {
 // start new Activity
 }
else {
 fragment.update(...);
}

   
To create different layouts with Fragments you can:
  • Use one activity, which displays two Fragments for tablets and only one on handsets devices. In this case you would switch the Fragments in the activity whenever necessary. This requires that the fragment is not declared in the layout file as such Fragments cannot be removed during runtime. It also requires an update of the action bar if the action bar status depends on the fragment.
  • Use separate activities to host each fragment on a handset. For example, when the tablet UI uses two Fragments in an activity, use the same activity for handsets, but supply an alternative layout that includes just one fragment. When you need to switch Fragments, start another activity that hosts the other fragment.
The second approach is the most flexible and in general preferable way of using Fragments. In this case the main activity checks if the detail fragment is available in the layout. If the detailed fragment is there, the main activity tells the fragment that is should update itself. If the detail fragment is not available the main activity starts the detailed activity.
It is good practice that Fragments do not manipulate each other. For this purpose a Fragment typically implements an interface to get new data from its host Activity.

No comments:

Post a Comment