Fragment的一些介绍

来源:互联网 发布:下载打印机软件 编辑:程序博客网 时间:2024/04/27 11:36

Fragment的产生:Fragment是android3.0的产物,在Android3.0之前,手机、平板电脑 的适配是个令人头疼的问题,Fragment也因此而产生。
那么我们一般使用Fragment来干些啥呢,通过查找资料得知,它有以下几个用法:


1.解决手机、平板电脑等各种设备的适配
2.多个Activity之间切换性能问题
3.模块化,即复用


Fragment的生命周期

——生命周期图片—-
这里写图片描述

—-Fragment与Activity生命周期对比图——
这里写图片描述


——生命周期—-

/**
* Called when a fragment is first attached to its activity.
* {@link #onCreate(Bundle)} will be called after this.
*/
onAttach:Fragment绑定到它的父Activity中的时候被调用,我们可以在这里保存它和Activity之间的引用。

/**
* Called to have the fragment instantiate its user interface view.
* This is optional, and non-graphical fragments can return null (which
* is the default implementation). This will be called between
* {@link #onCreate(Bundle)} and {@link #onActivityCreated(Bundle)}.
*/
onCreateView:在Fragment创建自己的视图结构的时候被调用,在这个方法中我们会载入Fragment的布局文件,在这个过程中,我们不能保证父Activity是否已经创建,所以有一些操作我们不能在这里完成.

/**
* Called to do initial creation of a fragment. This is called after
* {@link #onAttach(Activity)} and before
* {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)}
*

Note that this can be called while the fragment’s activity is
* still in the process of being created. As such, you can not rely
* on things like the activity’s content view hierarchy being initialized
* at this point. If you want to do work once the activity itself is
* created, see {@link #onActivityCreated(Bundle)}.
*/
onCreate:Fragment就是在这一步中产生的,可以用这个方法来启动其它线程来检索数据,比如从远程服务器中启动.

/**
* Called when the Fragment is visible to the user. This is generally
* tied to {@link Activity#onStart() Activity.onStart} of the containing
* Activity’s lifecycle.
*/
onStart:fragment完全可见时,但是还不能和用户进行交互。

/**
* Called when the fragment is visible to the user and actively running.
* This is generally
* tied to {@link Activity#onResume() Activity.onResume} of the containing
* Activity’s lifecycle.
*/
onResume:fragment完全可见,并且可以和用户进行交互,这时,Fragment就已经启动并运行起来了。

/**
* Called when the Fragment is no longer resumed. This is generally
* tied to {@link Activity#onPause() Activity.onPause} of the containing
* Activity’s lifecycle.
*/
onPause:Activity被暂停

/**
* Called when the Fragment is no longer started. This is generally
* tied to {@link Activity#onStop() Activity.onStop} of the containing
* Activity’s lifecycle.
*/
onStop:fragment完全停止。

/**
* Called when the fragment’s activity has been created and this
* fragment’s view hierarchy instantiated. It can be used to do final
* initialization once these pieces are in place, such as retrieving
* views or restoring state. It is also useful for fragments that use
* {@link #setRetainInstance(boolean)} to retain their instance,
* as this callback tells the fragment when it is fully associated with
* the new activity instance. This is called after {@link #onCreateView}
* and before {@link #onViewStateRestored(Bundle)}.
*/
onActivityCreated:当activity的onCreated()方法返回后调用此方法

/**
* Called when the view previously created by {@link #onCreateView} has
* been detached from the fragment. The next time the fragment needs
* to be displayed, a new view will be created. This is called
* after {@link #onStop()} and before {@link #onDestroy()}. It is called
* regardless of whether {@link #onCreateView} returned a
* non-null view. Internally it is called after the view’s state has
* been saved but before it has been removed from its parent.
*/
onDestroyView:系统销毁Fragment的视图显示,

/**
* Called when the fragment is no longer in use. This is called
* after {@link #onStop()} and before {@link #onDetach()}.
*/
onDestroy:Fragment完全销毁

/**
* Called when the fragment is no longer attached to its activity. This
* is called after {@link #onDestroy()}.
*/
onDetach:Fragment解绑


Activity与Fragment之间的通信

有以下几种方式:

1.通过handler传值
2.通过发送广播来传递消息
3.通过接口传递消息

以上三种方式,缺点都比较多,隆重介绍接下来的一种消息传递方式。
这种方式,我理解为是“第三者插足法”,即首先新建一个公共类AppContext,在这个类里面创建对Fragment或者是Activity进行初始化的方法:initFragment(Fragment fragment)、initActivity(Activity activity)。然后在Fragment或者是Activity里面调用AppContext类中的初始化方法。
这样,我们就可以在第三者,即AppContext里面进行传值了。
fragment里面:

AppContext.getInstance().initOrderFragment(this);

AppContext:

private class AppContext{     private OrderFragment mOrderFragment; /**     * 订单对象初始化     * @param fragment     */    public void initOrderFragment(OrderFragment fragment){        this.mOrderFragment = fragment;    }    /**     * 展示待竞价界面     */    public void showWaitBidMenu(){        if(mOrderFragment != null && mPager != null){            mPager.setCurrentItem(1);            mOrderFragment.showWaitBidMenu();        }    }}

这样就可以进行传值了。

0 0
原创粉丝点击