Fragment在xml中但作用不是显示view
2013-12-17
有时候会发现在xml文件中有使用fragment,但是却不是为了显示View,代码如下:
<FrameLayout 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=".AnimTest" > <fragment
android:id="@+id/loader_fra"
class="com.example.sdtest.LoadInXmlFragment"
android:layout_height="0dip"
android:layout_width="0dip"
android:visibility="gone"/> <ImageView
android:id="@+id/welcome_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/phone"
tools:ignore="ContentDescription" /> </FrameLayout>
可以看到上面xml文件中有LoadInXmlFragment,那我们来看看当进入和退出Activity时Activity和LoadInXmlFragment的生命周期, log如下:
// Enter Activity:
D/David-Activity( 7378): onCreate
D/David-Fragment( 7378): onAttach
D/David-Activity( 7378): onAttachFragment
D/David-Fragment( 7378): onCreate
D/David-Fragment( 7378): onCreateView
D/David-Fragment( 7378): onActivityCreated
D/David-Activity( 7378): onStart
D/David-Fragment( 7378): onStart
D/David-Activity( 7378): onResume
D/David-Fragment( 7378): onResume
// Exist Activity:
D/David-Fragment( 7378): onPause
D/David-Activity( 7378): onPause
D/David-Fragment( 7378): onStop
D/David-Activity( 7378): onStop
D/David-Fragment( 7378): onDestroy
D/David-Activity( 7378): onDestroy
D/David-Fragment表示LoadInXmlFragment中打印的log;
D/David-Activity表示Activity中打印的log;
可以看到Fragment基本是随着Activity执行自己的生命周期函数,因此这样使用Fragment的好处是:
1. 由于Fragment的上面周期和Activity保持一致,因此可以利用其完整的生命周期,将某些需要在Activity特定生命周期中执行的操作放在Fragment里面;
2. 保证较好的编码风格;
3. 目前就想到这么多,希望大家补充;
这种方式在Phonebook应用中很常见。
Fragment在xml中但作用不是显示view的更多相关文章
- web.xml中listener作用及使用
一.WebContextLoaderListener 监听类 它能捕捉到server的启动和停止,在启动和停止触发里面的方法做对应的操作! 它必须在web.xml 中配置才干使用,是配置监听类的 二. ...
- pom.xml中properties作用
可以在properties下声明相应的版本信息,然后在dependency下引用的时候用${spring-version}就可以引入该版本jar包了 <properties> <sp ...
- Android自己定义控件而且使其能够在xml中自己定义属性
为什么要自己定义View android开发中自己定义View的优点是显而易见的.比方说以下的这个顶部导航,它被设计出如今应用的每一个界面,但每次的内容却不尽同样.我们不能在每一个layout资源中都 ...
- web.xml中welcome-file-list的作用
今天尝试使用struts2+ urlrewrite+sitemesh部署项目,结果发现welcome-file-list中定义的欢迎页不起作用: <welcome-file-list> & ...
- web.xml中load-on-startup的作用
如下一段配置,熟悉DWR的再熟悉不过了:<servlet> <servlet-name>dwr-invoker</servlet-name> <ser ...
- web.xml中load-on-startup的作用(转)
web.xml中load-on-startup的作用 如下一段配置,熟悉DWR的再熟悉不过了:<servlet> <servlet-name>dwr-invoker< ...
- web.xml 中配置了error-page但不起作用问题
问题: 在web.xml 中配置了 error-page,但是好像不起作用,就是跳转不到指定的页面. 配置信息如下: <!-- 400错误 --> <error-page> & ...
- web.xml 中<context-param>与<init-param>的区别与作用
<context-param>的作用: web.xml的配置中<context-param>配置作用 1. 启动一个WEB项目的时候,容器(如:Tomcat)会去读它的配置文件 ...
- (转)web.xml中的contextConfigLocation在spring中的作用
(转)web.xml中的contextConfigLocation在spring中的作用 一.Spring如何使用多个xml配置文件 1.在web.xml中定义contextConfigLocat ...
随机推荐
- Activity中与ListActivity中使用listview区别
Activity中与ListActivity中使用listview区别 一.Activity中与ListActivity中使用listview区别(本身没多大区别,只是ListActivity在lis ...
- 转:copy initialization
转自: http://en.cppreference.com/w/cpp/language/copy_initialization copy initialization C++ C++ la ...
- 工作流学习——Activiti流程变量五步曲 (zhuan)
http://blog.csdn.net/zwk626542417/article/details/46648139 ***************************************** ...
- 转!sqlServer2000 表连接查询
在查询多个表时,我们经常会用“连接查询”.连接是关系数据库模型的主要特点,也是它区别于其它类型数据库管理系统的一个标志. 什么是连接查询呢? 概念:根据两个表或多个表的列之间的关系,从这些表中查询数据 ...
- mysql 保留两位小数
mysql保留字段小数点后两位小数用函数:truncate(s.price,2)即可.如果想用四舍五入的话用round(s.price,2).
- Java 数据类型之间的转换 拆分字符串 Date/Calendar的转换
数据类型转换 1. String - Int String str="123"; int i=1; int str=Integer.parseInt(str); String i= ...
- HBase之创建表
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; impo ...
- EF Core » 关系
对初学者理解关系很有用,先留下来,有时间边看边翻译. Caution 注意 This documentation is for EF Core. For EF6.x and earlier relea ...
- signalR的一些细节
获取根目录通过AppDomain.CurrentDomain.BaseDirectory 因为不能直接获取session ,使用的替代方案如下 private static Dictionary< ...
- js字符串函数之substring() substr()
substring 方法用于提取字符串中介于两个指定下标之间的字符 substring(start,end) 开始和结束的位置,从零开始的索引 参数 描述start 必需.一个非负的整 ...