1,简介

Fragement(碎片)允许将Activity拆分成多个完全独立封装的可重用组件,每个组件有它自己的生命周期和UI布局,由此可见,Fragement依赖于Activity,它的生命周期直接被其所属的宿主activity的生命周期影响。

形象的理解Fragement,手机屏幕如下图所示:

Fragement 具有以下优点:

  • 组件重用,多个Activity可重用同一个Fragement;
  • 为不同屏幕大小的设备创建动态的灵活的UI,在Activity运行过程中,可以添加、移除或者替换Fragment(add()、remove()、replace())

 

2,生命周期

Fragement的生命周期镜像它的宿主Activity的生命周期事件。若Activity进入active-resumed状态的时候,添加或者移除一个Fragement就会影响它自己的生命周期。

Fragement生命周期方面如下:

/**
* Listing 4-4: Fragment skeleton code
* Listing 4-5: Fragment lifecycle event handlers
*/
package com.paad.fragments; import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; public class MySkeletonFragment extends Fragment { // Called when the Fragment is attached to its parent Activity.
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// Get a reference to the parent Activity.
} // Called to do the initial creation of the Fragment.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Initialize the Fragment.
} // Called once the Fragment has been created in order for it to
// create its user interface.
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
// Create, or inflate the Fragment's UI, and return it.
// If this Fragment has no UI then return null.
return inflater.inflate(R.layout.my_fragment, container, false);
} // Called once the parent Activity and the Fragment's UI have
// been created.
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Complete the Fragment initialization �particularly anything
// that requires the parent Activity to be initialized or the
// Fragment's view to be fully inflated.
} // Called at the start of the visible lifetime.
@Override
public void onStart(){
super.onStart();
// Apply any required UI change now that the Fragment is visible.
} // Called at the start of the active lifetime.
@Override
public void onResume(){
super.onResume();
// Resume any paused UI updates, threads, or processes required
// by the Fragment but suspended when it became inactive.
} // Called at the end of the active lifetime.
@Override
public void onPause(){
// Suspend UI updates, threads, or CPU intensive processes
// that don't need to be updated when the Activity isn't
// the active foreground activity.
// Persist all edits or state changes
// as after this call the process is likely to be killed.
super.onPause();
} // Called to save UI state changes at the
// end of the active lifecycle.
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
// Save UI state changes to the savedInstanceState.
// This bundle will be passed to onCreate, onCreateView, and
// onCreateView if the parent Activity is killed and restarted.
super.onSaveInstanceState(savedInstanceState);
} // Called at the end of the visible lifetime.
@Override
public void onStop(){
// Suspend remaining UI updates, threads, or processing
// that aren't required when the Fragment isn't visible.
super.onStop();
} // Called when the Fragment's View has been detached.
@Override
public void onDestroyView() {
// Clean up resources related to the View.
super.onDestroyView();
} // Called at the end of the full lifetime.
@Override
public void onDestroy(){
// Clean up any resources including ending threads,
// closing database connections etc.
super.onDestroy();
} // Called when the Fragment has been detached from its parent Activity.
@Override
public void onDetach() {
super.onDetach();
}
}

生命周期要点:

  • 开始于绑定到它的你Activity,结束于从父Activity分离,对应于onAttach 和onDetach事件,通常情况下,onAttach用来获取一下Fragement的父Activity的引用,为进一步初始化做准备;
  • 不能依赖调用onDestroy方法来销毁它,因为此方面不定会被执行;
  • 与Activity一样,应该使用onCreate方法来初始化Fragement,在其生命周期内创建的作用域对象,且确保只创建一次;
  • 在onCreateView和onDestroyView上初始化和销毁UI;
  • 当Fragement暂停或停止时,保存所有的UI状态和持久化所有的数据。

3,使用Fragement

最佳实践是使用容器View来创建布局,将Fragement在运行时放入容器内

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"> <FrameLayout
android:id="@+id/ui_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" >
</FrameLayout> <FrameLayout
android:id="@+id/details_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="3"

    android:visibility =”gone”    --隐藏
/>
</LinearLayout>

 

在运行时使用Fragement transaction来动态填充布局,从而当配置改变时,能确保一致性,框架代码如下:

package com.paad.fragments;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle; public class MyFragmentActivity extends Activity { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); // Inflate the layout containing the Fragment containers
setContentView(R.layout.fragment_container_layout); FragmentManager fm = getFragmentManager(); // Check to see if the Fragment back stack has been populated
// If not, create and populate the layout.
DetailsFragment detailsFragment =
(DetailsFragment)fm.findFragmentById(R.id.details_container); if (detailsFragment == null) {
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.details_container, new DetailsFragment());
ft.add(R.id.ui_container, new MyListFragment());
ft.commit();
}
}
}

package com.paad.weatherstation;

import com.paad.fragments.R;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; public class DetailsFragment extends Fragment { public DetailsFragment() {
} // Called once the Fragment has been created in order for it to
// create its user interface.
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
// Create, or inflate the Fragment's UI, and return it.
// If this Fragment has no UI then return null.
return inflater.inflate(R.layout.details_fragment, container, false);
}
}

package com.paad.weatherstation;

import com.paad.fragments.R;

import android.annotation.SuppressLint;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; public class MyListFragment extends Fragment { public MyListFragment() {
} // Called once the Fragment has been created in order for it to
// create its user interface.
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
// Create, or inflate the Fragment's UI, and return it.
// If this Fragment has no UI then return null.
return inflater.inflate(R.layout.list_fragment, container, false);
}
}

Fragement 和Activity之间的接口:

  • 任何Fragement中可使用getActivity方法返回对父Activity的引用;
  • 在fragement需要和它的主Activity共享事件的地方,最好在Fragement中创建回调接口,而主Activity必须实现它来监听Fragement中的改变;
package com.paad.fragments;

import android.app.Activity;
import android.app.Fragment; /**
* MOVED TO PA4AD_Ch04_Seasons
*/
public class SeasonFragment extends Fragment { public interface OnSeasonSelectedListener {
public void onSeasonSelected(Season season);
} private OnSeasonSelectedListener onSeasonSelectedListener;
private Season currentSeason; @Override
public void onAttach(Activity activity) {
super.onAttach(activity); try {
onSeasonSelectedListener = (OnSeasonSelectedListener)activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() +
" must implement OnSeasonSelectedListener");
}
} private void setSeason(Season season) {
currentSeason = season;
onSeasonSelectedListener.onSeasonSelected(season);
} }

若理解有难道,可以了解一下JAVA编程思想中的上塑造型,内部类,接口方面的知识

Fragment (一)的更多相关文章

  1. 浅谈 Fragment 生命周期

    版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 源码:AndroidDemo/Fragment 文中如有纰漏,欢迎大家留言指出. Fragment 是在 Android 3.0 中 ...

  2. 札记:Fragment基础

    Fragment概述 在Fragment出现之前,Activity是app中界面的基本组成单位,值得一提的是,作为四大组件之一,它是需要"注册"的.组件的特性使得一个Activit ...

  3. EventBus实现activity跟fragment交互数据

    最近老是听到技术群里面有人提出需求,activity跟fragment交互数据,或者从一个activity跳转到另外一个activity的fragment,所以我给大家介绍一个开源项目,EventBu ...

  4. Android:Activity+Fragment及它们之间的数据交换.

    Android:Activity+Fragment及它们之间的数据交换 关于Fragment与Fragment.Activity通信的四种方式 比较好一点的Activity+Fragment及它们之间 ...

  5. Android中Fragment和ViewPager那点事儿(仿微信APP)

    在之前的博文<Android中使用ViewPager实现屏幕页面切换和引导页效果实现>和<Android中Fragment的两种创建方式>以及<Android中Fragm ...

  6. Android开发学习—— Fragment

    #Fragment* 用途:在一个Activity里切换界面,切换界面时只切换Fragment里面的内容* 生命周期方法跟Activity一致,可以理解把其为就是一个Activity* 定义布局文件作 ...

  7. Android中Fragment与Activity之间的交互(两种实现方式)

    (未给Fragment的布局设置BackGound) 之前关于Android中Fragment的概念以及创建方式,我专门写了一篇博文<Android中Fragment的两种创建方式>,就如 ...

  8. Android中Fragment的两种创建方式

    fragment是Activity中用户界面的一个行为或者是一部分.你可以在一个单独的Activity上把多个Fragment组合成为一个多区域的UI,并且可以在多个Activity中再使用.你可以认 ...

  9. Android Fragment 剖析

    1.Fragment如何产生?2.什么是Fragment Android运行在各种各样的设备中,有小屏幕的手机,超大屏的平板甚至电视.针对屏幕尺寸的差距,很多情况下,都是先针对手机开发一套App,然后 ...

  10. ILJMALL project过程中遇到Fragment嵌套问题:IllegalArgumentException: Binary XML file line #23: Duplicate id

    出现场景:当点击"分类"再返回"首页"时,发生error退出   BUG描述:Caused by: java.lang.IllegalArgumentExcep ...

随机推荐

  1. AOJ 0121 Seven Puzzle

    7 パズル 7 パズルは 8 つの正方形のカードとこれらのカードがぴたりと収まる枠で構成されています.それぞれのカードには.互いに区別できるように 0, 1, 2, ..., 7 と番号がつけられてい ...

  2. iOS开发笔记-两种单例模式的写法

    iOS开发笔记-两种单例模式的写法   单例模式是开发中最常用的写法之一,iOS的单例模式有两种官方写法,如下: 不使用GCD #import "ServiceManager.h" ...

  3. 《Apache服务之php/perl/cgi语言的支持》RHEL6——服务的优先级

    安装php软件包: 安装文本浏览器 安装apache的帮助文档: 测试下是否ok 启动Apache服务关闭火墙: 编辑一个php测试页测试下: perl语言包默认系统已经安装了,直接测试下: Apac ...

  4. [大牛翻译系列]Hadoop(13)MapReduce 性能调优:优化洗牌(shuffle)和排序阶段

    6.4.3 优化洗牌(shuffle)和排序阶段 洗牌和排序阶段都很耗费资源.洗牌需要在map和reduce任务之间传输数据,会导致过大的网络消耗.排序和合并操作的消耗也是很显著的.这一节将介绍一系列 ...

  5. C++ string的常用功能

    头文件为#include<string> string str,str1; char s[]; str.length和str.size()是一样的功能都是返回当前字符串的大小: str.e ...

  6. php ftp文件上传函数--新手入门参考

    在 php编程中,用ftp上传文件比较多见,这里分享个简单入门型的ftp上传实例. <?php /** * ftp上传文件 * 学习ftp函数的用法 */ // 定义变量 $local_file ...

  7. python杂记-5(装饰器)

    1.被装饰的函数有参数(一个参数): def w1(func): def inner(arg): # 验证1 # 验证2 # 验证3 return func(arg) return inner @w1 ...

  8. Posix 信号量

    作用 信号量的值为0或正整数,就像红灯与绿灯,用于指示当前是否可以接受任务. 信号量对进程和线程都适用. gcc编译时需加-lpthread 基本函数 信号量的相关函数与标准文件函数非常相似,可以理解 ...

  9. QUOTENAME函数的用法

    quotename函数的语法为:quotename('expression1','expression2') expression1:指的是需要被特殊处理的字符 expression2:例如{}.[] ...

  10. Eclipse中使用javap运行配置详解

    javap是sun提供的对class文件进行反编译的工具 1.配置Run---external tools---external tools configurations 选择Program 新建ja ...