}
1.3.9FragmentManagerImpl#enqueueAction
/**
- Adds an action to the queue of pending actions.
- @param action the action to add
- @param allowStateLoss whether to allow loss of state information
- @throws IllegalStateException if the activity has been destroyed
*/
public void enqueueAction(Runnable action, boolean allowStateLoss) {
if (!allowStateLoss) {
checkStateLoss();
}
synchronized (this) {
if (mDestroyed || mHost == null) {
throw new IllegalStateException(“Activity has been destroyed”);
}
if (mPendingActions == null) {
mPendingActions = new ArrayList();
}
mPendingActions.add(action);
if (mPendingActions.size() == 1) {
mHost.getHandler().removeCallbacks(mExecCommit);
mHost.getHandler().post(mExecCommit);
}
}
}
这里把操作添加到mPendingActions列表里去。并通过mHost.getHandler()获取Handler发送执行请求。从上面的分析知道,mHost就是Activity的HostCallbacks,构造方法中把Activity的mHandler传进去了,这里执行的mHost.getHandler()获取到的也就是Activity中的mHandler,这样做是因为需要在主线程中执行
final Handler mHandler = new Handler();
再看看mExecCommit中做了什么操作:
Runnable mExecCommit = new Runnable() {
@Override
public void run() {
execPendingActions();
}
};
/**
- Only call from main thread!
*/
public boolean execPendingActions() {
if (mExecutingActions) {
throw new IllegalStateException(“Recursive entry to executePendingTransactions”);
}
//再次检测是否主线程
if (Looper.myLooper() != mHost.getHandler().getLooper()) {
throw new IllegalStateException(“Must be called from main thread of process”);
}
boolean didSomething = false;
while (true) {
int numActions;
synchronized (this) {
//参数检测
if (mPendingActions == null || mPendingActions.size() == 0) {
break;
}
numActions = mPendingActions.size();
if (mTmpActions == null || mTmpActions.length < numActions) {
mTmpActions = new Runnable[numActions];
}
mPendingActions.toArray(mTmpActions);
mPendingActions.clear();
mHost.getHandler().removeCallbacks(mExecCommit);
}
mExecutingActions = true;
//遍历执行待处理的事务操作
for (int i=0; i<numActions; i++) {
mTmpActions[i].run();
mTmpActions[i] = null;
}
mExecutingActions = false;
didSomething = true;
}
doPendingDeferredStart();
return didSomething;
}
插入了事物之后,就是在主线程中把需要处理的事务统一处理,处理事务是通过执行mTmpActions[i].run()进行的,这个mTmpActions[i]就是前面我们通过enqueueAction方法插入的BackStackRecord,童鞋们可能没注意到,它可是一个Runnable,我们来看看它
的定义
final class BackStackRecord extends FragmentTransaction implements
FragmentManager.BackStackEntry, Runnable {
static final String TAG = FragmentManagerImpl.TAG;
… …
}
兜兜转转,我们又回到了BackStackRecord
1.3.10BackStackRecord#run
public void run() {
…
if (mManager.mCurState >= Fragment.CREATED) {
SparseArray firstOutFragments = new SparseArray();
SparseArray lastInFragments = new SparseArray();
calculateFragments(firstOutFragments, lastInFragments);
beginTransition(firstOutFragments, lastInFragments, false);
}
//遍历链表,根据cmd事务类型依次处理事务
Op op = mHead;
while (op != null) {
switch (op.cmd) {
case OP_ADD: {
Fragment f = op.fragment;
f.mNextAnim = op.enterAnim;
mManager.addFragment(f, false);
}
break;
case OP_REPLACE: {
Fragment f = op.fragment;
int containerId = f.mContainerId;
if (mManager.mAdded != null) {
for (int i = mManager.mAdded.size() - 1; i >= 0; i–) {
Fragment old = mManager.mAdded.get(i);
if (old.mContainerId == containerId) {
if (old == f) {
op.fragment = f = null;
} else {
if (op.removed == null) {
op.removed = new ArrayList();
}
op.removed.add(old);
old.mNextAnim = op.exitAnim;
if (mAddToBackStack) {
old.mBackStackNesting += 1;
}
mManager.removeFragment(old, mTransition, mTransitionStyle);
}
}
}
}
if (f != null) {
f.mNextAnim = op.enterAnim;
mManager.addFragment(f, false);
}
}
break;
case OP_REMOVE: {
Fragment f = op.fragment;
f.mNextAnim = op.exitAnim;
mManager.removeFragment(f, mTransition, mTransitionStyle);
}
break;
case OP_HIDE: {
Fragment f = op.fragment;
f.mNextAnim = op.exitAnim;
mManager.hideFragment(f, mTransition, mTransitionStyle);
}
break;
case OP_SHOW: {
Fragment f = op.fragment;
f.mNextAnim = op.enterAnim;
mManager.showFragment(f, mTransition, mTransitionStyle);
}
break;
case OP_DETACH: {
Fragment f = op.fragment;
f.mNextAnim = op.exitAnim;
mManager.detachFragment(f, mTransition, mTransitionStyle);
}
break;
case OP_ATTACH: {
Fragment f = op.fragment;
f.mNextAnim = op.enterAnim;
mManager.attachFragment(f, mTransition, mTransitionStyle);
}
break;
default: {
throw new IllegalArgumentException("Unknown cmd: " + op.cmd);
}
}
op = op.next;
}
mManager.moveToState(mManager.mCurState, mTransition,
mTransitionStyle, true);
if (mAddToBackStack) {
mManager.addBackStackState(this);
}
}
到这一步,提交的事务就被真正执行了,我们知道,即使commit了事务之后,也不是同步执行的,是通过Handler发送到主线程执行的。
所有事务的处理都是在run方法里面执行,但是我们留意到,想要搞清楚add、remove等事务背后真正做了什么,还需要深入了解FragmentManagerImpl。
#二、Fragment转场动画
Fragment的转场动画实现分为使用v4包和不使用v4包两种情况,不使用v4包的话,最低API Level需要是11。
2.1标准转场动画:
可以给Fragment指定标准的转场动画,通过**setTransition(int transit)**方法。
该方法可传入的三个参数是:
TRANSIT_NONE,
TRANSIT_FRAGMENT_OPEN,
TRANSIT_FRAGMENT_CLOSE
分别对应无动画、打开形式的动画和关闭形式的动画。
标准动画设置好后,在Fragment添加和移除的时候都会有。
2.2自定义转场动画
自定义转场动画是通过setCustomAnimations()方法,因为Fragment添加时可以指定加入到Back Stack中,所以转场动画有添加、移除、从Back stack中pop出来,还有进入四种情况。
注意setCustomAnimations()方法必须在add、remove、replace调用之前被设置,否则不起作用。
##2.3 android.app.Fragment
类参考:
Fragment:http://developer.android.com/reference/android/app/Fragment.html
FragmentTransaction:http://developer.android.com/reference/android/app/FragmentTransaction.html
不使用v4包的情况下(min API >=11)所对应的动画类型是Property Animation。
即动画资源文件需要放在*res\animator*目录下,且根标签是, , or **三者之一。
这一点也可以从Fragment中的这个方法看出:onCreateAnimator)(int transit, boolean enter, int nextAnim),返回值是Animator。
自定义转场动画时,四个参数的形式setCustomAnimations (int enter, int exit, int popEnter, int popExit)是API Level 13才有的,11只引入了两个动画的形式,即无法指定Back Stack栈操作时的转场动画。
代码例子:
private void addFragment() { if (null == mFragmentManager) {
mFragmentManager = getFragmentManager();
}
mTextFragmentOne = new MyFragmentOne();
FragmentTransaction fragmentTransaction = mFragmentManager
.beginTransaction(); // 标准动画 // fragmentTransaction // .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); // fragmentTransaction // .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); // fragmentTransaction // .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE); // 自定义动画 // API LEVEL 11
fragmentTransaction.setCustomAnimations(
R.animator.fragment_slide_left_enter,
R.animator.fragment_slide_right_exit); // API LEVEL 13 // fragmentTransaction.setCustomAnimations( // R.animator.fragment_slide_left_enter, // R.animator.fragment_slide_left_exit, // R.animator.fragment_slide_right_enter, // R.animator.fragment_slide_right_exit);
fragmentTransaction.add(R.id.container, mTextFragmentOne); // 加入到BackStack中
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
其中四个动画是从ApiDemos中拿来的:
fragment_slide_left_enter:
<?xml version="1.0" encoding="utf-8"?>fragment_slide_left_exit:
<?xml version="1.0" encoding="utf-8"?>fragment_slide_right_enter:
<?xml version="1.0" encoding="utf-8"?>fragment_slide_right_exit:
<?xml version="1.0" encoding="utf-8"?>android.support.v4.app.Fragment
Fragment:http://developer.android.com/reference/android/support/v4/app/Fragment.html
FragmentTransaction:http://developer.android.com/reference/android/support/v4/app/FragmentTransaction.html
使用v4包,Fragment的使用不再局限于API Level 11之上,低等级的API也可以使用,但是这时候转场动画的类型是View Animation。
动画资源放在**res\anim**路径下,和Activity的转场动画一样。
Fragment中的方法:onCreateAnimation)(int transit, boolean enter, int nextAnim)返回值Animation。
FragmentTransaction中的setCustomAnimations()方法,两参数类型和四参数类型都可用。
所以一般还是用v4包的这个版本,一是兼容性比较好,另外View Animation其实基本可以满足转场动画的需要。
代码例子:
private void addFragment() { if (null == mFragmentManager) {
mFragmentManager = getSupportFragmentManager();
}
mTextFragmentOne = new MyFragmentOne();
FragmentTransaction fragmentTransaction = mFragmentManager
.beginTransaction();
fragmentTransaction.setCustomAnimations(
四参数类型都可用。
所以一般还是用v4包的这个版本,一是兼容性比较好,另外View Animation其实基本可以满足转场动画的需要。
代码例子:
private void addFragment() { if (null == mFragmentManager) {
mFragmentManager = getSupportFragmentManager();
}
mTextFragmentOne = new MyFragmentOne();
FragmentTransaction fragmentTransaction = mFragmentManager
.beginTransaction();
fragmentTransaction.setCustomAnimations(