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. git 提交代码到github错误处理

    git push -u origin mastererror: The requested URL returned error: 403 Forbidden while accessing http ...

  2. django学习记录--第一个网页“hello django”

    一.安装django 下面两种方法任选其一 1.pip或easy_install 安装 pip install django easy_install django 2.到django官网(https ...

  3. 解决: org.iq80.leveldb.DBException: IO error: C:\data\trie\000945.sst: Could not create random access file.

    以太坊MPT树的持久化层是采用了leveldb数据库,然而在抽取MPT树代码运行过程中,进行get和write操作时却发生了错误: Caused by: org.fusesource.leveldbj ...

  4. 开源代码Window下搭建rtmp流媒体服务器

    合肥程序员群:49313181. 合肥实名程序员群:128131462 (不愿透露姓名和信息者勿加入) Q Q:408365330 E-Mail:egojit@qq.com 综合:有这样需求,将摄像头 ...

  5. WeCenter二次开发教程(一):熟悉模板结构

    <1>程序文件目录介绍: app – 应用目录 models – 模型目录 plugins – 插件目录 static – 静态文件 system – 系统目录 views – 模板目录 ...

  6. PHP 中:: -> self $this 操作符的区别

    访问PHP类中的成员变量或方法时, 如果被引用的变量或者方法被声明成const(定义常量)或者static(声明静态),那么就必须使用操作符::, 反之如果被引用的变量或者方法没有被声明成 const ...

  7. std::map用法

    STL是标准C++系统的一组模板类,使用STL模板类最大的好处就是在各种C++编译器上都通用.    在STL模板类中,用于线性数据存储管理的类主要有vector, list, map 等等.本文主要 ...

  8. JavaScript Array对象sort() 方法小结

    sort() 方法用于对数组的元素进行排序. 语法arrayObject.sort(sortfunction) 参数sortfunction 可选.规定排序顺序.必须是函数. 返回值对数组的引用.请注 ...

  9. 【EasyUI】combotree和combobox模糊查询

    这里说的模糊查询指在输入框输入,然后自动在下拉框中显示匹配结果,类似Google搜索提示 EasyUI库已经实现了combobox的查询过滤功能,但只能从头匹配,原因是EasyUI库的代码限制: fi ...

  10. Ubuntu 下 kazam 录屏 没声音解决方案

    以下内容参考https://www.youtube.com/watch?v=5NZ0qwp2L04,我做了些修改,让它好懂些. 在应用商店里搜索 PulseAudio Volume Control 在 ...