`
阿尔萨斯
  • 浏览: 4180092 次
社区版块
存档分类
最新评论

Android Fragment详解(二):Fragment创建及其生命周期

 
阅读更多

Fragments的生命周期

每一个fragments 都有自己的一套生命周期回调方法和处理自己的用户输入事件。 对应生命周期可参考下图:



创建片元(Creating a Fragment)

To create a fragment, you must create a subclass of Fragment (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 as onCreate(), onStart(), onPause(), and onStop(). In fact, if you're converting an existing Android application to use fragments, you might simply move code from your activity's callback methods into the respective callback methods of your fragment.

要创建一个fragment,必须创建一个fragment的子类(或是继承自它的子类)。fragment类的代码看起来很像activity。它与activity一样都有回调函数,例如onCreate(),onStart(),onPause(),和onStop()。事实上,如果你正在将一个现成的Android应用转而使用Fragment来实现,可以简单的将代码从activity的回调函数移植到各自的fragment回调函数中。

Usually, you should implement 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.
在创建fragment时系统会调用此方法。在实现代码中,你可以初始化想要在fragment中保持的那些必要组件,当fragment处于暂停或者停止状态之后可重新启用它们。

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.
在第一次为fragment绘制用户界面时系统会调用此方法。为fragment绘制用户界面,这个函数必须要返回所绘出的fragment的根View。如果fragment没有用户界面可以返回空。

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).
系统回调用该函数作为用户离开fragment的第一个预兆(尽管这并不总意味着fragment被销毁)。在当前用户会话结束之前,通常要在这里提交任何应该持久化的变化(因为用户可能不再返回)。

Most applications should implement at least these three methods for every fragment, but there are several other callback methods you should also use to handle various stages of the fragment lifecycle. All the lifecycle callback methods are discussed more later, in the section about Handling the Fragment Lifecycle.
大部分应用程序都应该至少为每个fragment实现这三个方法,但是还有许多其他用以操纵fragment生命周期中各个阶段的回调函数。所有生命周期中的回调函数在操纵fragment生命周期一节中稍后再做讨论。

There are also a few subclasses that you might want to extend, instead of the base Fragment class:
除了基类fragment,这里还有几个你可能会继承的子类:

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.
显示一个浮动的对话框。使用这个类创建对话框是使用Activity类对话框工具方法之外的另一个不错的选择,因为你可以把fragment对话框并入到由activity管理的fragments后台栈中,允许用户返回到一个已经摒弃的fragment。

ListFragment
Displays a list of items that are managed by an adapter (such as a SimpleCursorAdapter), similar to ListActivity. It provides several methods for managing a list view, such as the onListItemClick() callback to handle click events.
显示一个由适配器管理的条目列表(例如SimpleCursorAdapter),类似于ListActivity。并且提供了许多管理列表视图的函数,例如处理点击事件的onListItemClick()回调函数。

PreferenceFragment
Displays a hierarchy of Preference objects as a list, similar to PreferenceActivity. This is useful when creating a "settings" activity for your application.
显示一个Preference对象的体系结构列表,类似于preferenceActivity。这在为应用程序创建“设置”activity时是很实用的。


分享到:
评论

相关推荐

    Android Fragment的生命周期详解

    详解Android Fragment之二:Fragment的创建和生命周期  创建片元(Creating a Fragment)  To create a fragment, you must create a subclass of Fragment (or an existing subclass of it). The Fragment class...

    fragment声明周期详解Demo

    fragment生命周期详解

    Fragment的懒加载与生命周期详解

    Fragment的懒加载与生命周期详解什么是懒加载了解Fragment的生命周期onAttachonCreateonCreateViewonActivityCreatedonStartonResumeonPauseonStoponDestroyViewonDestroyonDetach懒加载的实现和封装 什么是懒加载 ...

    Fragment使用详解

    Fragment使用,Fragment生命周期

    Android Fragment动态创建详解及示例代码

    Android Fragment 动态创建 Fragment是activity的界面中的一部分或一种行为。可以把多个Fragment组合到一个activity中来创建一个多界面并且可以在多个activity中重用一个Fragment。可以把Fragment任务模块化的一段...

    Android开发教程之Fragment定义、创建与使用方法详解【包含Activity通讯,事务执行等】

    本文实例讲述了Android开发教程之Fragment定义、创建与使用方法。分享给大家供大家参考,具体如下: 概述 Fragment是activity的界面中的一部分或一种行为。你可以把多个Fragment们组合到一个activity中来创建一个...

    详解Android中Fragment的两种创建方式

    fragment是Activity中用户界面的...fragment的生命周期直接受其宿主activity的生命周期的影响。例如,一旦activity被暂停,它里面所有的fragment也被暂停,一旦activity被销毁,它里面所有的fragment也被销毁。 Androi

    Android中Fragment的解析和使用详解

    Android Fragment的生命周期和Activity类似,实际可能会涉及到数据传递,onSaveInstanceState的状态保存,FragmentManager的管理和Transaction,切换的Animation。 我们首先简单的介绍一下Fragment的生命周期。 ...

    Android Fragment(动态,静态)碎片详解及总结

    Android Fragment(动态,静态)碎片详解 一.Fragment的相关概念(一)Fragment的基础知识  Fragment是Android3.0新增的概念,中文意思是碎片,它与Activity十分相似,用来在一个 Activity中描述一些行为或一部分用户...

    Android Activity生命周期详解

    Activity 的生命周期。 一、理解Activity Activity是Android程序的4大组件之... 二、理解Activity的生命周期 手机最重要也是最基础的功能是打电话,也就意味着电话来的时候可能随时要把现在的程序暂停,如果电量不足

    Android横竖屏幕切换生命周期详解

    主要为大家详细介绍了Android横竖屏幕切换生命周期,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

    Android基础之Fragment与Activity交互详解

    今天继续讲解Fragment组件的特性,主要是跟Activity的交互和生命周期的关系,我们前面已经说过Fragment是依赖于Activity的,而且生命周期也跟Activity绑定一起。下面我们看看Fragment跟Activity的关系。1、为...

    Android Activity横竖屏切换生命周期详解

    关于Activity生命周期和横竖屏切换时,生命周期的执行过程,网上有很多文章。但是都写的很模糊,并且不完善。一般的我们去切换屏幕方向都是不希望Activity被重新创建,这时就需要对一些属性进行设置,或者使用代码...

    Android开发 Activity和Fragment详解

    下面我们来看一下Activity的生命周期: 2.Activity的用法 1)启动、关闭Activity // 首先需要创建启动的Activity对应的Intent Intent intent = new Intent(MainActivity.this, TwoActivity.class

    Android编程中activity的完整生命周期实例详解

    本文实例分析了Android编程中activity的完整生命周期。分享给大家供大家参考,具体如下: android中 activity有自己的生命周期,对这些知识的学习可以帮助我们在今后写程序的时候,更好的理解其中遇到的一些错误。这...

    两分钟让你彻底明白Android Activity生命周期的详解(图文介绍)

    大家好,今天给大家详解一下Android中Activity的生命周期,我在前面也曾经讲过这方面的内容,但是像网上大多数文章一样,基本都是翻译Android API,过于笼统,相信大家看了,会有一点点的帮助 ,但是还不能完全吃透...

    Android学习资料

    包括:android gravity 和 android layout_Gravity,Bundle,ContentResolver,cursor,Fragments ,Fragments,inflater ,Fragment,数据查询query函数参数解析, 【Cursor】 类的介绍,android 获得通话记录,Handler ,...

Global site tag (gtag.js) - Google Analytics