[Android]Fragment源代码分析(二) 状态
我们上一讲,抛出来一个问题,就是当Activity的onCreateView的时候,是怎样构造Fragment中的View參数。要回答这个问题我们先要了解Fragment的状态,这是Fragment管理中很重要的一环。我们先来看一下FragmentActivity提供的一些核心回调:
@Override
protected void onCreate(Bundle savedInstanceState) {
mFragments.attachActivity(this, mContainer, null);
// Old versions of the platform didn't do this!
if (getLayoutInflater().getFactory() == null) {
getLayoutInflater().setFactory(this);
} super.onCreate(savedInstanceState);
....
mFragments.dispatchCreate();
}
我们跟入mFragments.dispatchCreate方法中:
public void dispatchCreate() {
mStateSaved = false;
moveToState(Fragment.CREATED, false);
}
我们看到,对于FragmentManager来说,做了一次状态转换。我上一篇说过FragmentManager是及其重要的类,它承担了Fragment管理最为核心的工作。它有它自身的状态机,而它的状态,能够理解为与Activity本身基本同步。
在Fm里面维护自己的一个状态,当你导入一个Fragment的时候,Fm的目的,就是为了让Fragment和自己的状态基本保持一致.
void moveToState(int newState, int transit, int transitStyle, boolean always) {
if (mActivity == null && newState != Fragment.INITIALIZING) {
throw new IllegalStateException("No activity");
}
if (!always && mCurState == newState) {
return;
}
mCurState = newState;
if (mActive != null) {
boolean loadersRunning = false;
for (int i = 0; i < mActive.size(); i++) {
Fragment f = mActive.get(i);
if (f != null) {
moveToState(f, newState, transit, transitStyle, false);
if (f.mLoaderManager != null) {
loadersRunning |= f.mLoaderManager.hasRunningLoaders();
}
}
}
...
}
}
我们看到,FragmentManager的每一次状态变更,都会引起mActive里面的Fragment的状态变更。而mActive是全部纳入FragmentManager管理的Fragment容器。我们来看一下Fragment的几个状态:
static final int INITIALIZING = 0; // Not yet created.
static final int CREATED = 1; // Created.
static final int ACTIVITY_CREATED = 2; // The activity has finished its creation.
static final int STOPPED = 3; // Fully created, not started.
static final int STARTED = 4; // Created and started, not resumed.
static final int RESUMED = 5; // Created started and resumed.
能够看出实际上,你的状态越靠后你的状态值越大,实际上在Fm的管理中,也巧妙的用到了这一点。
if (f.mState < newState)
{
...
} else {
...
}
对于f.mState<newState能够理解为创造的过程。同一时候我们也能找到我们上一篇文章的问题的答案:
if (f.mFromLayout) {
// For fragments that are part of the content view
// layout, we need to instantiate the view immediately
// and the inflater will take care of adding it.
f.mView = f.performCreateView(
f.getLayoutInflater(f.mSavedFragmentState), null,
f.mSavedFragmentState);
if (f.mView != null) {
f.mInnerView = f.mView;
f.mView = NoSaveStateFrameLayout.wrap(f.mView);
if (f.mHidden)
f.mView.setVisibility(View.GONE);
f.onViewCreated(f.mView, f.mSavedFragmentState);
} else {
f.mInnerView = null;
}
}
f.mFromLayout代表的是你这个Fragment的生成是否是从layout.xml文件里生成的。而它的View的生成是调用performCreateView来生成的。
View performCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (mChildFragmentManager != null) {
mChildFragmentManager.noteStateNotSaved();
}
return onCreateView(inflater, container, savedInstanceState);
}
对,这里就是我们很熟悉的onCreateView回调的出处。
当然我们如今还是属于Fragment.INITIALIZING这个状态。但实际上,我们在调用Fragment的时候FragmentManageer已经进入了Create状态。也就是说newState參数应该是Create才对。
所以我们接着代码往下走:
case Fragment.CREATED:
if (newState > Fragment.CREATED) {
if (!f.mFromLayout) {
ViewGroup container = null;
if (f.mContainerId != 0) {
container = (ViewGroup) mContainer
.findViewById(f.mContainerId);
if (container == null && !f.mRestored) {
throwException(new IllegalArgumentException(
"No view found for id 0x"
+ Integer
.toHexString(f.mContainerId)
+ " ("
+ f.getResources()
.getResourceName(
f.mContainerId)
+ ") for fragment " + f));
}
}
f.mContainer = container;
f.mView = f.performCreateView(
f.getLayoutInflater(f.mSavedFragmentState),
container, f.mSavedFragmentState);
if (f.mView != null) {
f.mInnerView = f.mView;
f.mView = NoSaveStateFrameLayout.wrap(f.mView);
if (container != null) {
Animation anim = loadAnimation(f, transit,
true, transitionStyle);
if (anim != null) {
f.mView.startAnimation(anim);
}
container.addView(f.mView);
}
if (f.mHidden)
f.mView.setVisibility(View.GONE);
f.onViewCreated(f.mView, f.mSavedFragmentState);
} else {
f.mInnerView = null;
}
} f.performActivityCreated(f.mSavedFragmentState);
if (f.mView != null) {
f.restoreViewState(f.mSavedFragmentState);
}
f.mSavedFragmentState = null;
}
我们看到实际上这段代码是对FragmentManager状态是Create以上状态且Fragment的导入并非採用layout.xml方式导入的处理。这是为什么呢?由于在onCreate之后,基本上你的控件已经在Create状态的时候生成的差点儿相同了,你所要做的就是在生成的控件中找到Fragment相应的容器,然后装入你的控件。
同一时候,我们也看到了对Fragment的动画处理:
if (f.mView != null) {
f.mInnerView = f.mView;
f.mView = NoSaveStateFrameLayout.wrap(f.mView);
if (container != null) {
Animation anim = loadAnimation(f, transit,
true, transitionStyle);
if (anim != null) {
f.mView.startAnimation(anim);
}
container.addView(f.mView);
}
if (f.mHidden)
f.mView.setVisibility(View.GONE);
f.onViewCreated(f.mView, f.mSavedFragmentState);
} else {
f.mInnerView = null;
}
而这样的动画的处理和參数的配置,我们留到后面讲到Fragment事务的时候再说。
[Android]Fragment源代码分析(二) 状态的更多相关文章
- [Android]Fragment源代码分析(三) 事务
Fragment管理中,不得不谈到的就是它的事务管理,它的事务管理写的很的出彩.我们先引入一个简单经常使用的Fragment事务管理代码片段: FragmentTransaction ft = thi ...
- [Android]Volley源代码分析(二)Cache
Cache作为Volley最为核心的一部分,Volley花了重彩来实现它.本章我们顺着Volley的源代码思路往下,来看下Volley对Cache的处理逻辑. 我们回忆一下昨天的简单代码,我们的入口是 ...
- Android HttpURLConnection源代码分析
Android HttpURLConnection源代码分析 之前写过HttpURLConnection与HttpClient的差别及选择.后来又分析了Volley的源代码. 近期又遇到了问题,想在V ...
- Android 消息处理源代码分析(1)
Android 消息处理源代码分析(1) 在Android中,通常被使用的消息队列的代码在文件夹\sources\android-22\android\os下,涉及到下面几个类文件 Handler.j ...
- Android HandlerThread 源代码分析
HandlerThread 简单介绍: 我们知道Thread线程是一次性消费品,当Thread线程运行完一个耗时的任务之后.线程就会被自己主动销毁了.假设此时我又有一 个耗时任务须要运行,我们不得不又 ...
- Android KLog源代码分析
Android KLog源代码分析 Android KLog源代码分析 代码结构 详细分析 BaseLog FileLog JsonLog XmlLog 核心文件KLogjava分析 遇到的问题 一直 ...
- android开发源代码分析--多个activity调用多个jni库的方法
android开发源代码分析--多个activity调用多个jni库的方法 有时候,我们在开发android项目时会遇到须要调用多个native c/jni库文件,下面是本人以前实现过的方法,假设有知 ...
- Android 消息处理源代码分析(2)
Android 消息处理源代码分析(1)点击打开链接 继续接着分析剩下的类文件 Looper.java public final class Looper { final MessageQueue m ...
- Appium Android Bootstrap源代码分析之启动执行
通过前面的两篇文章<Appium Android Bootstrap源代码分析之控件AndroidElement>和<Appium Android Bootstrap源代码分析之命令 ...
随机推荐
- hdu 3715
一个很简单的2-sat的题: 不过比较难想到: 其实也不是很难,可能接触的少了吧! #include<cstdio> #include<vector> #define maxn ...
- 对于 APM 用户的一次真实调查分析(下)
一.前言 对 APM 用户的一次真实调查分析(上)中,我们主要聊到了现阶段国外 APM 行业对各个企业的渗透率.大部分使用 APM 工具的企业规模以及 APM 工具在用户心中的地位等问题,有兴趣的朋友 ...
- LINUX Shell 下求两个文件交集和差集的办法
http://blog.csdn.net/autofei/article/details/6579320 假设两个文件FILE1和FILE2用集合A和B表示,FILE1内容如下: a b c e d ...
- [状压dp]HDU5045 Contest
题意: n和人做m道题, 每俩人做的题数不能相差一题以上.(也就是每n道题分别由n个人完成) 给n个人分别做m道题的概率, 求完成m题的最大期望 $1\le N \le 10$ 注意!!! fil ...
- 安装edX DevStack
概述 edX Developer Stack 就是通常我们所说的Devstack,是为本地开发所设计的一个Vagrant实例. Devstack: 和产品(edx Product Stack)对系统的 ...
- [jobdu]把数组排成最小的数
这道题见过,就是把相加的结果作为比较来排序就行了.注意的是comp函数里面要用const引用.而且c++里的字符串直接操作(读入和相加)也很方便. #include <iostream> ...
- VirtualBox虚拟vdi磁盘扩容
http://blog.chinaunix.net/uid-25627207-id-3342576.html
- 使用Html.fromHtml将html格式字符串应用到textview上面
在android中,有一个容易遗忘的Html.fromhtml方法,意思是可以将比如文本框中的字符串进行HTML格式化,支持的还是很多的, 但要注意的是要在string.xml中用<!--cda ...
- 公司的SVN服务器改变了IP地址,请问以前下载的代码如何同步,
工作根目录上 右键->TortoiseSVN->Relocate,修改URL 更新前先备份!
- poj3254Corn Fields(状压)
http://poj.org/problem?id=3254 第一个状压题 思路挺好想 用二进制表示每行的状态 然后递推 用左移 右移来判断是不是01间隔出现 c大神教的 我的代码WA在这个地方了.. ...