fragment的生命周期,它与Activity生命周期的关联;

来源:互联网 发布:手机屏幕直播录像软件 编辑:程序博客网 时间:2024/05/17 00:55

一、概论

随着Android系统的多样化,不仅仅在手机上,在平板、电视等设备上应用的也越来越多,这样就会有一个需要适应不同屏幕的问题。在Android3.0之后,谷歌推出了Fragment,Fragment在Android中被称为碎片。
我们可以把Fragment看作是Activity的一个界面或者组成部分,而且Fragment具有与Activity很相似的生命周期,我们可以在Activity中动态的添加或者移除某个Fragment。

二、生命周期

(一)Fragment的生命周期

谷歌技术文档上Fragment的生命周期截图为:



为了更直观的看到Fragment切换时候的生命周期,我们在每个生命周期方法中打印一句Log日志,更直观的看到Fragment在创建、隐藏、显示、销毁时候的生命周期。

1.第一次打开应用:



2.按home键隐藏应用:


3.从后台切换回来时:


4.退出应用时:



(二)Fragment的生命周期与Activity之间的关系

我们会发现跟Activity对比,Fragment会多几个生命周期,等下会介绍这你个不同生命周期方法的作用,先来看一张谷歌技术文档上Fragment与Activity生命周期之间的关系图:



onCreate( )、onStart( )、 onStop( )等生命周期方法是跟Activity的生命周期一一对应的,这里不做详细介绍,最主要的就是跟Activity不一样的生命周期。

(1)onAttach( ):当Fragment与Activity绑定、关联的时候调用;
(2)onCreateView( ):创建该Fragment对应的视图,并把视图返回给调用者,与onCreate( )的区别是你可以在onCreate( )中初始化与View无关的东西;
(3)onActivityCreated( ):在Activity完成其onCreate()方法后调用;
(4)onDestroyView( ):当Fragment销毁视图的时候调用;
(5)onDetach( ):当Fragment与Activity脱离关系的时候调用;

三、Fragment与Activity的关联

如图:我们要实现这种效果,上面的标题与下面的内容区域,我们分别用两个Fragment来显示。



Fragment与Activity关联主要有两种方式,一种是通过在Activity的布局文件中写入fragment控件,使用name属性指定一个Fragment;另一种是在java代码中动态的添加与删除Fragment。

(一)在Activity布局中关联Fragment

这种关联方式比较简单,只需要在Activity的布局文件中关联上需要显示的fragment控件,就是把Fragment当成普通的View一样声明在Activity的布局文件中,而Activity的java代码只需要加载自己的这个布局即可,优点是比较快捷,可以提高复用性与维护性,缺点是移除与替换比较麻烦,xml布局文件如下:
[html] view plain copy
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.     <fragment  
  7.         android:id="@+id/fragmentone"  
  8.         android:name="com.example.fragmentdemo.FragmentOne"  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="wrap_content" />  
  11.     <fragment  
  12.         android:id="@+id/fragmenttwo"  
  13.         android:name="com.example.fragmentdemo.FragmentTwo"  
  14.         android:layout_width="match_parent"  
  15.         android:layout_height="match_parent" />  
  16. </LinearLayout>  

FragmentOne跟FragmentTwo的布局这里就不贴出来了,就是两个简单的TextView而已。

(二)实用java代码关联Fragment

实用java代码可以动态的添加、删除与替换Fragment,实用性更广一些。
首先我们在Activity的布局文件中添加两个FrameLayout控件,用来存放要显示的Fragment:
[html] view plain copy
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.     <FrameLayout  
  7.         android:id="@+id/framelayoutone"  
  8.         android:layout_width="match_parent"  
  9.         android:layout_height="wrap_content" >  
  10.     </FrameLayout>  
  11.     <FrameLayout  
  12.         android:id="@+id/framelayouttwo"  
  13.         android:layout_width="match_parent"  
  14.         android:layout_height="match_parent" >  
  15.     </FrameLayout>  
  16. </LinearLayout>  
对应Activity中的java代码为:
[java] view plain copy
  1. package com.example.fragmentdemo;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.FragmentManager;  
  5. import android.app.FragmentTransaction;  
  6. import android.os.Bundle;  
  7. import android.view.Window;  
  8. import android.widget.FrameLayout;  
  9.   
  10. public class MainActivity extends Activity {  
  11.       
  12.     private FragmentOne fragmentOne;  
  13.     private FragmentTwo fragmentTwo;  
  14.       
  15.     @Override  
  16.     protected void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.           
  19.         requestWindowFeature(Window.FEATURE_NO_TITLE);   
  20.         setContentView(R.layout.activity_main);  
  21.           
  22.         // 初始化要显示的两个Fragment  
  23.         fragmentOne = new FragmentOne();  
  24.         fragmentTwo = new FragmentTwo();  
  25.           
  26.         // 得到Fragment的管理者FragmentManager  
  27.         FragmentManager fm = getFragmentManager();  
  28.           
  29.         // 开启一个事务  
  30.         FragmentTransaction ft = fm.beginTransaction();  
  31.           
  32.         /** 
  33.          * 在FrameLayout中加载我们要显示的Fragment,这里我们使用了replace方法,下面我会介绍它与add的区别 
  34.          * 参数: 
  35.          * containerViewId:需要存放到哪个控件中; 
  36.          * fragment:需要显示的Fragment; 
  37.          * tag:给Fragment定义一个标签,方便我们取出使用等。 
  38.          */  
  39.         ft.replace(R.id.framelayoutone, fragmentOne, "ONE");  
  40.         ft.replace(R.id.framelayouttwo, fragmentTwo, "TWO");  
  41.           
  42.         // 最后,千万不要忘记了使用commit提交,这点有点类似数据库中的事务的使用  
  43.         ft.commit();      
  44.     }  
  45. }  

其中的布局,Fragment我们都可以进行替换,所以就达到了动态创建Fragment的目的。
事务中其他的方法还有:
ft.add( ):添加一个Fragment到Activity中;
ft.remove( ):把Fragment从Activity中移除;
ft.hide( ):隐藏当前的Fragment,不会销毁它;
ft.show( ):显示隐藏的Fragment;
上面我们为什么要使用ft.replace( )呢?因为ft.replace( )是ft.remove( )与ft.add( )的合体,相当于先移除Fragment后,再添加一个Fragment。
0 0
原创粉丝点击