Android,Now becomes the world's most popularmobile OS.
Android powers more than a billion phones and tablets around theworld. It’s customizable, yet easy to use, and since it's made byGoogle, it works seamlessly with all your favoriteGoogle
Fragment
represents a behavior or a portion of user interface in an
Activity
.You can combine multiple fragments in a single activity to build amulti-pane UI and reuse a fragment in multiple activities. You canthink of a fragment as a modular section of an activity, which hasits own lifecycle, receives its own input events, and which you canadd or remove while the activity is running (sort of like a "subactivity" that you can reuse in different activities).
A fragment must always be embedded in an activity and thefragment's lifecycle is directly affected by the host activity'slifecycle. For example, when the activity is paused, so are allfragments in it, and when the activity is destroyed, so are allfragments. However, while an activity is running (it is in the resumed lifecycle state), you can manipulate each fragmentindependently, such as add or remove them. When you perform such afragment transaction, you can also add it to a back stack that'smanaged by the activity—each back stack entry in the activity is arecord of the fragment transaction that occurred. The back stackallows the user to reverse a fragment transaction (navigatebackwards), by pressing the Back button.
When you add a fragment as a part of your activity layout, it livesin a
ViewGroup
inside the activity's view hierarchy and thefragment defines its own view layout. You can insert a fragmentinto your activity layout by declaring the fragment in theactivity's layout file, as a element, or from your application codeby adding it to an existing
ViewGroup
. However, a fragment is not required to be apart of the activity layout; you may also use a fragment withoutits own UI as an invisible worker for the activity.
How to create afragment.
To create a fragment, you must create a subclass ofFragment
(or an existing subclass of it). The Fragment
class has code that looks a lot like an Activity
.It contains callback methods similar to an activity, such asonCreate()
, onStart()
, onPause()
, and onStop()
. In fact, if you're converting an existingAndroid application to use fragments, you might simply move codefrom your activity's callback methods into the respective callbackmethods of your fragment.
Usually, you should implement at least the following lifecyclemethods:
- The system calls this when creating the fragment. Within yourimplementation, you should initialize essential components of thefragment that you want to retain when the fragment is paused orstopped, then resumed.
-
The system calls this when it's time for the fragment to drawits user interface for the first time. To draw a UI for yourfragment, you must return a
View
from this method that is the root of your fragment's layout. Youcan return null if the fragment does not provide a UI. - The system calls this method as the first indication that theuser is leaving the fragment (though it does not always mean thefragment is being destroyed). This is usually where you shouldcommit any changes that should be persisted beyond the current usersession (because the user might not come back).
onCreate()
onCreateView()
onPause()
Most applications should implement at least these three methodsfor every fragment, but there are several other callback methodsyou should also use to handle various stages of the fragmentlifecycle. All the lifecycle callback methods are discussed in moredetail in the section about Handling the Fragment Lifecycle.
There are also a few subclasses that you might want to extend,instead of the base Fragment
class:
-
Displays a floating dialog. Using this class to create a dialogis a good alternative to using the dialog helper methods in the
Activity
class, because you can incorporate a fragment dialog into the backstack of fragments managed by the activity, allowing the user toreturn to a dismissed fragment. -
Displays a list of items that are managed by an adapter (suchas a
SimpleCursorAdapter
), similar toListActivity
. It provides several methods for managing alist view, such as theonListItemClick()
callback to handle click events. -
Displays a hierarchy of
Preference
objects as a list, similar toPreferenceActivity
. This is useful when creating a"settings" activity for your application.
The lifecycle of a fragment which are like the followings:
DialogFragment
ListFragment
PreferenceFragment