Fragments for All
1 / 19
1 So, What’s a Fragment?
A fragment is a class implementing a portion of an activity.
• A fragment represents a particular operation or interface running within a larger activity.
• Fragments enable more modular activity design, making it easier to adapt an application to different screen orientations
and multiple screen sizes.
• Fragments must be embedded in activities; they cannot run independent of activities.
• Most fragments define their own layout of views that live within the activity’s view hierarchy.
– However, a fragment can implement a behavior that has no user interface component.
• A fragment has its own lifecycle, closely related to the lifecycle of its host activity.
• A fragment can be a static part of an activity, instantiated automatically during the activity’s creation.
• Or, you can create, add, and remove fragments dynamically in an activity at run-time.
2 Fragments: Implemented in Honeycomb (3.0) or Later
Fragments were added to the Android API in Honeycomb, API 11.
The primary classes related to fragments are:
android.app.Fragment
The base class for all fragment definitions
android.app.FragmentManager
The class for interacting with fragment objects inside an activity
android.app.FragmentTransaction
The class for performing an atomic set of fragment operations
3 Fragments: Implemented in Donut (1.6) or Later
Google provides the Compatibility Package, a Java library that you can include in an application, implementing support
for fragments and other Honeycomb features (loaders).
• You can use the Compatibility Package with applications targeting 1.6 (API 4) or later.
• For each of the classes in the Compatibility Package, the APIs work almost exactly the same as their counterparts in
the latest Android platform. Therefore, you can usually refer to the online documentation for information about the
supported APIs.
Note
If you write an application using the Compatibility Package, your application uses the class definitions from the Com-
patibility Package even when running on a device with 3.0 or later installed. The package does not try to switch to the
device’s “native” implementation of the classes.
评论4
最新资源