【Android基础】Fragment 详解之Fragment生命周期

上一篇文章简单介绍了一下Fragment,这一篇文章会详细的说一下Fragment的生命周期和创建一个用户界面。

Fragment的主要功能就是创建一个View,并且有一个生命周期来管理这个View的创建和销毁。Fragment的生命周期与Activity的生命周期类似,都会有一些回调方法,你所做的工作就是利用好这些生命周期方法,在恰当的方法中做恰当的工作。

Fragment的生命周期与Activity的状态图如下:

activity_fragment_lifecycle

左侧是Activity的生命周期状态,右侧对应的是这个状态下回执行Fragment的哪些生命周期方法。可以看到Fragment生命周期函数与Activity生命周期函数很多名字都是一样的,对应的功能也类似,只不过在Created状态和Destroyed状态比Activity增加了一些方法。下面就说一下这几个增加的生命周期方法:

  • onAttach():
    当fragment与它所在的Activity关联起来的时候调用。
  • onCreatView():
    当需要创建一个与Fragment关联的View时候会调用,这个方法会返回一个View,这个View会被添加到Activity的View树中,如果你不想Fragment显示一个View就返回null。
  • onDestroyView():
    当与Fragment关联的那个View(在onCreatView()方法中创建的)与Fragment解除关联,从View树中移除的时候调用,在下次Fragment需要显示一个View的时候会重新调用onCreatView方法。
  • onDetach():
    当Fragment与之前onAttach()是关联起来的那个Activity解除关系的时候调用。

与Activity类似,Fragment可以停留(长时间存在)的三个状态:

  • Resumed:
    Fragment的运行状态,此时Fragment处于运行状态,并且可以与用户之间进行交互,类似Activity的Resumed状态。
  • Paused:
    有其他Activity获取焦点,前台运行,Fragment所在的Activity失去焦点,部分的显示在前台Activity下面。
  • Stopped:
    Fragment不再可见,此时的情形可能是Fragment所在的Activity已经stopped了,或者fragment从Activity中移除到Fragment回退栈中。一个Stopped状态的Fragment不没有被销毁,还在存活状态,它的状态和内部信息被系统记录和保存,只是不可见,不可交互,此时很可能会被系统回收。

与Activity类似,你可以利用Bundle来记录Fragment的状态,当Activity被销毁需要记录Fragment状态,并且在Activity重新创建的时候恢复Fragment的状态。你可以保存Fragment的状态在Fragment的onSaveInstanceState()回调方法中,在onCteat()、onCreatView()或者onActivityCreated()方法中进行恢复。

在生命周期中Activity与Fragment的最大不同之处是回退栈是相互独立的,Activity的回退栈是系统来管理的,Fragment的回退栈是被宿主Activity来管理的,也就是说你可以来进行控制(调用addToBackStack()).

注意:在Fragment中你如果要获取一个Context对象,你可以调用getActivity()方法,但是调用getActivity()方法必须要在Fragment于Activity关联起来之后,否则getActivity()返回为null。

上面说的都是Fragment的一些生命周期相关知识,下面来看看如何创建Fragment并且添加一个View给Fragment。

要想给Fragment添加一个View,你就必须重写onCreateView方法,在这个方法中创建一个View并且返回,这个View是Fragment的根View。在这个方法中系统给你提供了LayoutInflater对象,通过这个对象你可以从xml文件中创建一个View,代码如下:

publicstaticclassExampleFragmentextendsFragment{ 
    @Override 
    publicView onCreateView(LayoutInflaterinflater,ViewGroupcontainer,
                             BundlesavedInstanceState){
        // Inflate the layout for this fragment 
        returninflater.inflate(R.layout.example_fragment,container,false);
    } 
}

在onCreatView方法中container参数是一个ViewGroup,这个ViewGroup是从Activity传递过来的,是Fragment的View将要嵌入的那个父View。这里需要注意一下LayoutInflater的inflate函数,在这里第三个参数是false。

public View inflate (int resource, ViewGroup root, boolean attachToRoot)

Parameters
resource ID for an XML layout resource to load (e.g., R.layout.main_page)
root Optional view to be the parent of the generated hierarchy (if attachToRoot is true), or else simply an object that provides a set of LayoutParams values for root of the returned hierarchy (if attachToRoot is false.)
attachToRoot Whether the inflated hierarchy should be attached to the root parameter? If false, root is only used to create the correct subclass of LayoutParams for the root view in the XML.

Returns

  • The root View of the inflated hierarchy. If root was supplied and attachToRoot is true, this is root; otherwise it is the root of the inflated XML file.

在这解释一下inflate的三个参数含义:

resource:是所要解析的layout文件ID;

root:一个ViewGroup,attachToRoot为true时会作为从xml文件解析出来的view的根View,如果attachToRoot为false时仅仅提供一些布局的参数给返回的rootView;

attachToRoot:决定解析的view是否会绑定到root参数提供的view上。

以上就是Fragment的生命周期和如何创建一个Fragment的View。

 

关注微信公众平台:程序员互动联盟(coder_online),你可以第一时间获取原创技术文章,和(java/C/C++/Android/Windows/Linux)技术大牛做朋友,在线交流编程经验,获取编程基础知识,解决编程问题。程序员互动联盟,开发人员自己的家。

【答疑解惑】java中的全局变量

posted @ 2015-09-03 21:50  wuhao_blog  阅读(2713)  评论(1编辑  收藏  举报