APP Activities Fragments
http://developer.android.com/guide/components/fragments.html
1. Fragments
A Fragment represents a behavior or a portion of the user interface in an Activity.
a modular section of an activity.
A Fragment must always be embedded in an activity, and its lifecycle is directly affected by the host activity's lifecycle.
Each back stack entry in the activity is a record of the fragment transaction that occured.
Adding a fragment:
(1) declare the fragment in the activity's layout file, as a <fragment> element.
(2) add a fragment by adding it to an existing ViewGroup .
A Fragment is not required to be a part of the activity layout, it can be used as an invisible worker without its own UI.
2. Creating a fragment
implementing at least the following lifecycle methods.
onCreate()
- The system calls this when creating the fragment. Within your implementation, you should initialize essential components of the fragment that you want to retain when the fragment is paused or stopped, then resumed.
onCreateView()
- The system calls this when it's time for the fragment to draw its user interface for the first time. To draw a UI for your fragment, you must return a
View
from this method that is the root of your fragment's layout. You can return null if the fragment does not provide a UI. onPause()
- The system calls this method as the first indication that the user is leaving the fragment (though it does not always mean the fragment is being destroyed). This is usually where you should commit any changes that should be persisted beyond the current user session (because the user might not come back).
There are also a few subclasses that you might want to extend, instead of the base Fragment
class:
DialogFragment
- Displays a floating dialog. Using this class to create a dialog is a good alternative to using the dialog helper methods in the
Activity
class, because you can incorporate a fragment dialog into the back stack of fragments managed by the activity, allowing the user to return to a dismissed fragment. ListFragment
- Displays a list of items that are managed by an adapter (such as a
SimpleCursorAdapter
), similar toListActivity
. It provides several methods for managing a list view, such as theonListItemClick()
callback to handle click events. PreferenceFragment
- Displays a hierarchy of
Preference
objects as a list, similar toPreferenceActivity
. This is useful when creating a "settings" activity for your application.
3. Adding a user interface
To provide a layout for a fragment, you must implement the onCreateView()
callback method, which the Android system calls when it's time for the fragment to draw its layout. Your implementation of this method must return a View
that is the root of your fragment's layout.