1. 自定义lifecycleoffragment布局文件
  2. 在main_activity布局中引用自定义的fragment布局
  3. 到logcat中查看程勋运行的结果
  4. 代码如下:

    自定义的fragment布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00FF00"
>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="这是展示LifeCycleOfFragment,请到logcat中查看"
android:textColor="#000000"
android:textSize="25sp"
/>

</LinearLayout>

Fragment的java类

package com.cm.lifecycleoffragment;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; /**
* Created by Administrator on 2016/1/2.
*/
public class LifeCycleOfFragment extends Fragment{ @Override
public void onAttach(Activity activity) {
super.onAttach(activity); Log.d("lifecycleoffragment","onAttach()");
} @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); Log.d("lifecycleoffragment","onCreate()");
} @Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { Log.d("lifecycleoffragment","onCreateView()");
return inflater.inflate(R.layout.lifecycleof_fragment,container,false); } @Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.d("lifecycleoffragment", "onActivityCreated()");
} @Override
public void onStart() {
super.onStart();
Log.d("lifecycleoffragment", "onStart()");
} @Override
public void onResume() {
super.onResume();
Log.d("lifecycleoffragment", "onResume()");
} @Override
public void onPause() {
super.onPause();
Log.d("lifecycleoffragment", "onPause()");
} @Override
public void onStop() {
super.onStop();
Log.d("lifecycleoffragment", "onStop()");
} @Override
public void onDestroyView() {
super.onDestroyView(); Log.d("lifecycleoffragment", "onDestroyView()");
} @Override
public void onDestroy() {
super.onDestroy();
Log.d("lifecycleoffragment", "onDestroy()");
} @Override
public void onDetach() {
super.onDetach(); Log.d("lifecycleoffragment", "onDetach()");
} }

在main_activity的布局文件中引用fragment

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
> <fragment
android:name="com.cm.lifecycleoffragment.LifeCycleOfFragment"
android:id="@+id/lifecycleof_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
/> </RelativeLayout>

Logcat打印的程序运行过程:

按home键返回的运行过程:

再次进入程序的运行过程:

按ctrl+f11程序的运行过程:

按back键的运行过程:

以上过程充分展示fragment的生命周期。

Fragment的生命周期(三)的更多相关文章

  1. Android系列之Fragment(二)----Fragment的生命周期和返回栈

    ​[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/ ...

  2. Android学习笔记(六)Fragment的生命周期

    在上一篇博文中对Fragment做了简单的介绍,现在再来探讨一下Fragment的生命周期. 一.Fragment的几种状态: 与Activity类似,Fragment也有一下几种状态: · 活动状态 ...

  3. Fragment 的生命周期及使用方法详解

    Fragment 的基础知识介绍 1.1 概述 1.1.1 特性 By hebang32624 Fragment 是 activity 的界面中的一部分或一种行为.可以把多个 Fragment 组合到 ...

  4. 【Android】11.4 Fragment及其生命周期

    分类:C#.Android.VS2015: 创建日期:2016-02-22 一.简介 Android从3.0开始引入了fragment的概念,主要是为了支持在大屏幕上实现更为动态和灵活的UI设计,比如 ...

  5. Fragment的生命周期

    Fragment的生命周期: 1. onAttach():Fragment对象跟Activity关联时 2. onCreate():Fragment对象的初始创建时 3. onCreateView() ...

  6. 友盟页面统计 - 关于Viewpager中的Fragment的生命周期

    Activity和Fragment各自理论上的生命周期 Activity的生命周期是较为经典也最清晰的,在此不表: Fragment从出现到广泛运用也有一段时间了,其标准生命周期也仅比Activity ...

  7. fragment的生命周期及其各个周期方法的作用

    先上生命周期图: Fragment的生命周期图: 与Activity的生命周期对比图: 由于Fragment是嵌在Activity中使用的,故其生命周期也是依赖于Activity的周期的,或者说Fra ...

  8. Activity与Fragment的生命周期

    今天看到一张图,详细描述了Activity和Fragment的生命周期,好资源共享咯!

  9. Fragment的生命周期&同一Activity下不同Fragment之间的通信

    Android开发:碎片Fragment完全解析(2) Fragment的生命周期 和Activity一样,Fragment也有自己的生命周期,理解Fragment的生命周期非常重要,我们通过代码的方 ...

随机推荐

  1. 不使用插件实现对WordPress默认编辑器的增强

    四处寻觅无果.无意看了一下wordpress官方的API函数.苍天有眼啊!原来,后台的编辑器可以插入很多增强功能.果断卸载掉CK and SyntaxHighlighter编辑器插件.事实上,Word ...

  2. selenium提供了三种模式的断言:assert,verify,waitfor

    Assert:失败时,该测试将终止 Verify:失败时,该测试继续执行,并将错误日志记录在日显示屏 Waitfor:等待某些条件变为真,一般使用在AJAX应用程序的测试 断言常用的有,具体见如下:a ...

  3. lua实现私有函数

    本文是原创文章,如需转载,请注明文章出处 要用lua实现私有函数,关键就是使用metatable的特性来实现. Test.lua: local v = {};v.x = 100;v.y = 200; ...

  4. CSS样式应用

    CSS样式应用的方法: (1)行内样式,将css样式直接放到标签当中,一般都是放入标签的style属性中,它是最方便的一种样式,也是最不方便修改的样式.如下: (2)内嵌式,通过将css写在网页源文件 ...

  5. linux shell技巧

    一.在SHELL编程中,经常要处理一些字符串变量.比如,计算长度啊.截取子串啊.字符替换啊等等,常常要用到awk.expr.sed.tr等命令.下面给大家介绍个简单的字符串处理方法,用不着嵌套复杂的子 ...

  6. js多行省略

    $(function (){ // var $introduce = $(".c-introduce").html(); // $new_introduce = $introduc ...

  7. CSS3新增的选择器和属性

    <!doctype html>无标题文档 一.新增的选择器 CSS3新增的属性选择器 {除ie6外的大部分浏览器支持) 序号 选择器 含义 实例 1 E[att^="val&qu ...

  8. c# applibrary实现一个Sheet表中存放多张DataTable数据

    1.工具类(applibrary.dll) public class ExcelHelper { /// <summary> /// 文件名 /// </summary> pu ...

  9. css3过渡

    语法格式: transition:属性名 完成时间 速度曲线 何时开始 transition:width 2s ease-in 3s: width 2s 整个过渡效果持续的时间 ease-in 指定了 ...

  10. Android系统的架构

    android的系统架构和其操作系统一样,采用了分层的架构.从架构图看,android分为四个层,从高层到低层分别是应用程序层.应用程序框架层.系统运行库层和linux核心层. 1.应用程序 Andr ...