Fragment的一些介绍
2016-03-18 13:56
295 查看
Fragment的产生:Fragment是android3.0的产物,在Android3.0之前,手机、平板电脑 的适配是个令人头疼的问题,Fragment也因此而产生。
那么我们一般使用Fragment来干些啥呢,通过查找资料得知,它有以下几个用法:
1.解决手机、平板电脑等各种设备的适配
2.多个Activity之间切换性能问题
3.模块化,即复用
—-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解绑
1.通过handler传值
2.通过发送广播来传递消息
3.通过接口传递消息
以上三种方式,缺点都比较多,隆重介绍接下来的一种消息传递方式。
这种方式,我理解为是“第三者插足法”,即首先新建一个公共类AppContext,在这个类里面创建对Fragment或者是Activity进行初始化的方法:initFragment(Fragment fragment)、initActivity(Activity activity)。然后在Fragment或者是Activity里面调用AppContext类中的初始化方法。
这样,我们就可以在第三者,即AppContext里面进行传值了。
fragment里面:
AppContext:
这样就可以进行传值了。
那么我们一般使用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(); } } }
这样就可以进行传值了。
相关文章推荐
- Android Fragment 基本了解(图文介绍)
- Android应用开发中Fragment与Activity间通信示例讲解
- Android的Fragment的生命周期各状态和回调函数使用
- 浅谈Android App开发中Fragment的创建与生命周期
- Android Fragment的使用方法(翻译)
- 实例探究Android应用编写时Fragment的生命周期问题
- 使用Fragment来处理Andoird app的UI布局的实例分享
- Android应用开发中Fragment间通信的实现教程
- Android 中 Fragment的使用大全
- Android基于ViewPager Fragment实现选项卡
- Android中Fragment与Activity的生命周期对比
- 实例讲解Android应用开发中Fragment生命周期的控制
- Android学习之Fragment
- Android中Fragment的生命周期与返回栈的管理
- Android基础之使用Fragment适应不同屏幕和分辨率(分享)
- 深入浅析 Android Fragment(上篇)
- Android中给fragment写入参数的轻量开发包FragmentArgs简介
- 深入浅析Android Fragment(下篇)
- FrameLayout和Fragment处理Android应用UI布局实例
- 浅谈Android app开发中Fragment的Transaction操作