How to use Fragment in Android

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 apps.

From android 3.0(API level 11),a new activity was born. It isfragment,a Fragmentrepresents 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 Fragmentclass 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:

onCreate()
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.
onCreateView()
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 Viewfrom this method that is the root of your fragment's layout. Youcan return null if the fragment does not provide a UI.
onPause()
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).

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 Fragmentclass:

DialogFragment
Displays a floating dialog. Using this class to create a dialogis a good alternative to using the dialog helper methods in the Activityclass, because you can incorporate a fragment dialog into the backstack of fragments managed by the activity, allowing the user toreturn to a dismissed fragment.
ListFragment
Displays a list of items that are managed by an adapter (suchas a SimpleCursorAdapter), similar to ListActivity. It provides several methods for managing alist view, such as the onListItemClick() callback to handle click events.
PreferenceFragment
Displays a hierarchy of Preference objects as a list, similar to PreferenceActivity. This is useful when creating a"settings" activity for your application.
The lifecycle of a fragment which are like the followings:
How <wbr>to <wbr>use <wbr>Fragment <wbr>in <wbr>Android


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值