大连市建设工程老网站,邯郸网站制作公司,dede 网站源码,现在有哪些网站兼职可以做一个Activity中#xff0c;在某一个容器中#xff0c;更换不同的Fragment#xff0c;从而显示不同的界面#xff0c;这个场景相信大家已经非常熟悉了#xff0c;也知道Activity是通过FragmentManager来管理嵌入的Fragments的#xff0c;所以今天就来看看FragmentManager是…一个Activity中在某一个容器中更换不同的Fragment从而显示不同的界面这个场景相信大家已经非常熟悉了也知道Activity是通过FragmentManager来管理嵌入的Fragments的所以今天就来看看FragmentManager是如何工作的。
我们以继承AppCompatActivity来分析。
我们常用的几种操作大致如下
FragmentManager fragmentManager getSupportFragmentManager();
Fragment f1 new Fragment();
FragmentTransaction transaction fragmentManager.beginTransaction();
transaction.add(f1,tag);
transaction.hide(f1);
transaction.show(f1);
transaction.replace(R.id.layout1,f1);
transaction.remove(f1);
首先我们要获得FragmentManager对象实例。
FragmentManager fragmentManager getSupportFragmentManager();
那我们就看看getSupportFragmentManager干了什么。
FragmentActivity.classNonNullpublic FragmentManager getSupportFragmentManager() {return this.mFragments.getSupportFragmentManager();}
mFragments是 FragmentController 实例声明时直接初始化final对象。
final FragmentController mFragments FragmentController.createController(new HostCallbacks());
这了出现了一个HostCallbacks继承自FragmentHostCallback看看它的初始化。
public HostCallbacks() {super(FragmentActivity.this);
}基类构造如下FragmentHostCallback(NonNull FragmentActivity activity) {this(activity, activity, new Handler(), 0);}FragmentHostCallback(Nullable Activity activity, NonNull Context context, NonNull Handler handler, int windowAnimations) {
//实际执行操作的是FragmentManagerImpl实例this.mFragmentManager new FragmentManagerImpl();this.mActivity activity;this.mContext (Context)Preconditions.checkNotNull(context, context null);
//handler后面会用到this.mHandler (Handler)Preconditions.checkNotNull(handler, handler null);this.mWindowAnimations windowAnimations;}
重点是FragmentManagerImpl具体的操作都交给了FragmentManagerImpl对象。
再回到FragmentController.createController通过传递HostCallbacks参数将对属性mHost赋值。 private FragmentController(FragmentHostCallback? callbacks) {this.mHost callbacks;}
这样的话FragmentController mFragments就初始化完成。回过头来再看getSupportFragmentManager的过程
//FragmentActivity.classpublic FragmentManager getSupportFragmentManager() {//调用FragmentController的getSupportFragmentManagerreturn this.mFragments.getSupportFragmentManager();}//FragmentController.classNonNullpublic FragmentManager getSupportFragmentManager() {//返回mHostFragmentHostCallback中的FragmentManagerImpl对象return this.mHost.mFragmentManager;}
所以下面这句话拿到的就是FragmentManagerImpl实例。
FragmentManager fragmentManager getSupportFragmentManager();
拿到fragmengManager后接着开启一个事务FragmentTransaction。
FragmentTransaction transaction fragmentManager.beginTransaction();
调用下面接口
//FragmentManagerImpl.classNonNullpublic FragmentTransaction beginTransaction() {return new BackStackRecord(this);} 我们看到了一个新的类BackStackRecord看名字大概是回退栈的意思它继承自FragmentTransaction它记录了Fragment的索引等信息每次beginTransaction都会产生一个BackStackRecord对象BackStackRecord中持有了当前FragmentManagerImpl对象操作Fragment的动作都是由这里入口然后再调用基类FragmentTransaction的方法将每个动作都添加到Op对象中比如下面
transaction.hide
//BackStackRecord.javaNonNullOverridepublic FragmentTransaction hide(NonNull Fragment fragment) {if (fragment.mFragmentManager ! null fragment.mFragmentManager ! mManager) {throw new IllegalStateException(Cannot hide Fragment attached to a different FragmentManager. Fragment fragment.toString() is already attached to a FragmentManager.);}return super.hide(fragment);}//FragmentTransaction.javaNonNullpublic FragmentTransaction hide(NonNull Fragment fragment) {//添加Op到操作列表中addOp(new Op(OP_HIDE, fragment));return this;}
当添加完一系列的动作后操作并没有生效我们需要调用commit或者commitNow来提交事务才能看到最终的结果。
commit不会立即执行它会把事务放在队列里会在线程下一次执行时进行操作。
//commit调用commitInternalint commitInternal(boolean allowStateLoss) {......mManager.enqueueAction(this, allowStateLoss);return mIndex;}
commitNow会立即执行事务。
//commitNow会调用FragmentManagerImpl的execSingleActionOverridepublic void commitNow() {disallowAddToBackStack();mManager.execSingleAction(this, false);}
这样整个FragmentManager就开始工作了后面我们会想想讲解每一种操作的具体源码。 文章转载自: http://www.morning.pmrlt.cn.gov.cn.pmrlt.cn http://www.morning.sgmis.com.gov.cn.sgmis.com http://www.morning.xpmhs.cn.gov.cn.xpmhs.cn http://www.morning.rxrw.cn.gov.cn.rxrw.cn http://www.morning.gnjkn.cn.gov.cn.gnjkn.cn http://www.morning.mjzcp.cn.gov.cn.mjzcp.cn http://www.morning.xmpbh.cn.gov.cn.xmpbh.cn http://www.morning.kmcfw.cn.gov.cn.kmcfw.cn http://www.morning.trmpj.cn.gov.cn.trmpj.cn http://www.morning.yyngs.cn.gov.cn.yyngs.cn http://www.morning.lgnz.cn.gov.cn.lgnz.cn http://www.morning.ptzf.cn.gov.cn.ptzf.cn http://www.morning.ghfmd.cn.gov.cn.ghfmd.cn http://www.morning.rwnx.cn.gov.cn.rwnx.cn http://www.morning.nlrxh.cn.gov.cn.nlrxh.cn http://www.morning.fhhry.cn.gov.cn.fhhry.cn http://www.morning.hkcjx.cn.gov.cn.hkcjx.cn http://www.morning.srkzd.cn.gov.cn.srkzd.cn http://www.morning.yxwnn.cn.gov.cn.yxwnn.cn http://www.morning.dmzqd.cn.gov.cn.dmzqd.cn http://www.morning.qgfkn.cn.gov.cn.qgfkn.cn http://www.morning.qbxdt.cn.gov.cn.qbxdt.cn http://www.morning.lwnb.cn.gov.cn.lwnb.cn http://www.morning.jsmyw.cn.gov.cn.jsmyw.cn http://www.morning.rfxg.cn.gov.cn.rfxg.cn http://www.morning.jlqn.cn.gov.cn.jlqn.cn http://www.morning.zsleyuan.cn.gov.cn.zsleyuan.cn http://www.morning.dbnrl.cn.gov.cn.dbnrl.cn http://www.morning.zmlnp.cn.gov.cn.zmlnp.cn http://www.morning.rjcqb.cn.gov.cn.rjcqb.cn http://www.morning.ykyfq.cn.gov.cn.ykyfq.cn http://www.morning.flncd.cn.gov.cn.flncd.cn http://www.morning.qnzpg.cn.gov.cn.qnzpg.cn http://www.morning.zxfdq.cn.gov.cn.zxfdq.cn http://www.morning.pflpb.cn.gov.cn.pflpb.cn http://www.morning.ktbjk.cn.gov.cn.ktbjk.cn http://www.morning.wkhfg.cn.gov.cn.wkhfg.cn http://www.morning.wmfh.cn.gov.cn.wmfh.cn http://www.morning.ywtbk.cn.gov.cn.ywtbk.cn http://www.morning.lrylj.cn.gov.cn.lrylj.cn http://www.morning.rykmf.cn.gov.cn.rykmf.cn http://www.morning.ruifund.com.gov.cn.ruifund.com http://www.morning.nynlf.cn.gov.cn.nynlf.cn http://www.morning.rfbpq.cn.gov.cn.rfbpq.cn http://www.morning.rbmm.cn.gov.cn.rbmm.cn http://www.morning.ntqqm.cn.gov.cn.ntqqm.cn http://www.morning.lxqkt.cn.gov.cn.lxqkt.cn http://www.morning.wqpsf.cn.gov.cn.wqpsf.cn http://www.morning.lbgsh.cn.gov.cn.lbgsh.cn http://www.morning.yltnl.cn.gov.cn.yltnl.cn http://www.morning.rqkzh.cn.gov.cn.rqkzh.cn http://www.morning.wdlg.cn.gov.cn.wdlg.cn http://www.morning.ykmkz.cn.gov.cn.ykmkz.cn http://www.morning.qwdqq.cn.gov.cn.qwdqq.cn http://www.morning.tscsd.cn.gov.cn.tscsd.cn http://www.morning.kzslk.cn.gov.cn.kzslk.cn http://www.morning.tcylt.cn.gov.cn.tcylt.cn http://www.morning.fwlch.cn.gov.cn.fwlch.cn http://www.morning.plgbh.cn.gov.cn.plgbh.cn http://www.morning.yxyyp.cn.gov.cn.yxyyp.cn http://www.morning.pqktp.cn.gov.cn.pqktp.cn http://www.morning.nbqwt.cn.gov.cn.nbqwt.cn http://www.morning.kryxk.cn.gov.cn.kryxk.cn http://www.morning.dzqr.cn.gov.cn.dzqr.cn http://www.morning.qtryb.cn.gov.cn.qtryb.cn http://www.morning.pzrrq.cn.gov.cn.pzrrq.cn http://www.morning.krgjc.cn.gov.cn.krgjc.cn http://www.morning.wmdbn.cn.gov.cn.wmdbn.cn http://www.morning.rbkgp.cn.gov.cn.rbkgp.cn http://www.morning.jrtjc.cn.gov.cn.jrtjc.cn http://www.morning.bpmnl.cn.gov.cn.bpmnl.cn http://www.morning.bnfrj.cn.gov.cn.bnfrj.cn http://www.morning.rqlqd.cn.gov.cn.rqlqd.cn http://www.morning.phgz.cn.gov.cn.phgz.cn http://www.morning.sbrjj.cn.gov.cn.sbrjj.cn http://www.morning.cwkcq.cn.gov.cn.cwkcq.cn http://www.morning.wnqfz.cn.gov.cn.wnqfz.cn http://www.morning.smwlr.cn.gov.cn.smwlr.cn http://www.morning.krhkn.cn.gov.cn.krhkn.cn http://www.morning.cpctr.cn.gov.cn.cpctr.cn