fragment的生命周期,你需了解的一切
来源:互联网 发布:淘宝女装拍照动作 编辑:程序博客网 时间:2024/06/01 20:40
前言
- 在之前做的一个小项目中,因为对fragment的生命周期不是很熟悉,在注册和注销广播处出现了bug。
先了解下fragment app和 support v4 的区别
1.最低支持版本不同
android.app.Fragment 兼容的最低版本是 android:minSdkVersion=”11” 即3.0版
android.support.v4.app.Fragment 兼容的最低版本是android:minSdkVersion=”4” 即1.6版
2.需要导jar包
fragment android.support.v4.app.Fragment 需要引入包android-support-v4.jar
3.在Activity中取的方法不同 android.app.Fragment(ListFragment)getFragmentManager().findFragmentById(R.id.userList) 获得 ,继承Activity
android.support.v4.app.Fragment(ListFragment)getSupportFragmentManager().findFragmentById(R.id.userList) 获得 ,需要继承android.support.v4.app.FragmentActivity
下面是一个简单的布局让我们去了解下fragment的生命周期吧
以android.support.v4.app.Fragment为例
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin"> <LinearLayout android:id="@+id/list_item" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <Button android:id="@+id/next" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="下一个页面"/> <Button android:id="@+id/first" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="第一个fragment"/> <Button android:id="@+id/second" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="第二个fragment"/> </LinearLayout> <FrameLayout android:id="@+id/fragment" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/list_item" android:layout_centerHorizontal="true" > </FrameLayout> </RelativeLayout>
我们就以第一个fragment的生命周期为例,并且是默认加载的一个。
1. 当程序启动时即在MainActivity,在logcat可以看到:
2 . 当切换到第二个fragment时, logcat无输出。
当点击跳转到下一个页面(SecondActivity)的时候,在logcat可以看到:
3 . 当点击返回或者退回键的时候, 在logcat可以看到:
4.当按Home键的时候, 在logcat可以看到:
5.当我退出程序时,在logcat可以看到:
相信大家对fragment的生命周期也有一定的了解了吧。
如此,后来我解决了注册和注销广播的问题如下:
因为我只是在第一个fragment注册了广播,因此:
我在该fragment的onstart方法和onresume方法注册了广播,并且在onpause方法注销了广播。
个人建议一般选择在onresume方法onpause方法分别注册和注销广播。
源码 :https://github.com/zhuangzhitu/fragment
- fragment的生命周期,你需了解的一切
- 你真的了解Fragment的生命周期吗?
- Fragment---Fragment你应该知道的一切
- Android Fragment 你应该知道的一切
- 【Android】Fragment 你应该知道的一切
- Android Fragment 你应该知道的一切
- Android Fragment 你应该知道的一切
- Android Fragment 你应该知道的一切
- Android Fragment 你应该知道的一切
- Android Fragment 你应该知道的一切
- Android Fragment 你应该知道的一切
- Android Fragment 你应该知道的一切
- Android Fragment 你应该知道的一切
- Android Fragment 你应该知道的一切
- Android Fragment 你应该知道的一切
- Android Fragment 你应该知道的一切
- Android Fragment 你应该知道的一切
- Android Fragment 你应该知道的一切
- Xcode中指令集相关选项
- 织梦后台编辑文章时内容为空,可数据库里有内容,读取正常的问题解决办法
- iOS 字体的加粗和其他样式的效果
- Make
- 【Win32笔记一】Windows程式关键字
- fragment的生命周期,你需了解的一切
- Dubbo和Spring Cloud
- Scala学习笔记(四) ---- 基本类型和操作与函数式对象
- 数据库的运维策略脚本篇SQL SERVER(内附脚本,无私分享)
- 修改.bashrc文件
- gulp前端自动化构建工具(二):gulp插件/gulp模块介绍
- MSVCRTD.lib(crtexew.obj) : error LNK2019: 无法解析的外部符号 _WinMain@16,该符号在函数 ___tmainCRTStartup 中被引用
- Salt-API安装配置及使用
- 单点登录思路