Skip to content
YoKey edited this page Jun 27, 2017 · 36 revisions

前言:核心概括

  • loadRootX()系列方法,操作的对象是 孩子Fragment,一般在saveInstanceState==null时load;
  • startX()popX()find/getX()系列方法,操作的对象是 兄弟Fragment;
  • popChildX()find/getChildX()系列方法,操作的对象是 孩子Fragment。

通过上面3个概念配合详细的API以及栈视图,就可以轻松开发出单Activity+多Fragment结构的App。

Application初始化API

  Fragmentation.builder()
        // 设置 栈视图 模式为 悬浮球模式   SHAKE: 摇一摇唤出   NONE:隐藏
        .stackViewMode(Fragmentation.BUBBLE)
        // ture时,遇到异常:"Can not perform this action after onSaveInstanceState!"时,会抛出
        // false时,不会抛出,会捕获,可以在handleException()里监听到
        .debug(BuildConfig.DEBUG)
        // 在debug=false时,即线上环境时,上述异常会被捕获并回调ExceptionHandler
        .handleException(new ExceptionHandler() {
             @Override
             public void onException(Exception e) {
                 // 建议在该回调处上传至我们的Crash监测服务器 
                 // 以Bugtags为例子: 手动把捕获到的 Exception 传到 Bugtags 后台。
                 // Bugtags.sendException(e);
             }
         })
         .install();

SupportActivity独有API

// 查看栈视图Dialog
showFragmentStackHierarchyView();
// 打印查看栈视图Log
logFragmentStackHierarchy(TAG);

// 当Fragment根布局没有设定background属性时,
// Fragmentation默认使用Theme的android:windowbackground作为Fragment的背景,
// 可以通过该方法改变Fragment背景。
setDefaultFragmentBackground(@DrawableRes int backgroundRes);

API一览(除了特别标注的API,对于其他API,SupportActivity和SupportFragment都具有)

1、装载根Fragment,一般在saveInstanceState==null时load

// 装载根Fragment, 即Activity内的第一个Fragment 或 Fragment内的第一个子Fragment
loadRootFragment(int containerId, SupportFragment toFragment)
loadRootFragment(int containerId, SupportFragment toFragment, boolean addToBackStack, boolean allowEnterAnim)

// 装载多个根Fragment,用于同级Fragment的场景,详情见新Demo的MainActivity
loadMultipleRootFragment(int containerId, int showPosition, SupportFragment... toFragments);

附:同级Fragment场景下的切换

// show一个Fragment,hide一个Fragment; 主要用于类似微信主页那种 切换tab的情况
showHideFragment(SupportFragment showFragment, SupportFragment hideFragment);

2、启动Fragment

如果在Activity使用,则本质是FragmentActivity.getSupportFragmentManager().getTopFragment().start(f)

如果在Fragment中使用,则是Fragment.getFragmentManager().start(f)

假设栈内有[A,B],当想加载C时,那么 B.start(C)等同于A.start(C),原因是start内部还是通过FragmentManager操作C入栈的

// 启动新的Fragment,启动者和被启动者是在同一个栈的
start(SupportFragment fragment)

// 以某种启动模式,启动新的Fragment
start(SupportFragment fragment, int launchMode)

// 启动新的Fragment,并能接收到新Fragment的数据返回
startForResult(SupportFragment fragment,int requestCode)

// 启动目标Fragment,并关闭当前Fragment;不要尝试pop()+start(),动画会有问题
startWithPop(SupportFragment fragment)

// 1.0.0 New:  你可以使用extraTransaction() + start() 来实现上面的各种startXX()设置更多功能
supportFragment.extraTransaction()
                .setTag(tag)  // 自定义tag
                .addSharedElement(xx).setLaunchMode(SINGLETASK).withPop(true).forResult(1)
                .start();
              //.dontAddToBackStack()
              //.add()
              //.remove(f) ...

下面的方法是SupportFragment才有的:

// replace方式启动目标Fragment,配合replaceLoadRootFragment()使用
replaceFragment(SupportFragment toFragment, boolean addToBack)

// 在onCreate/onCreateView/onActivityCreated中使用 start()/pop()/showHideFragment(),请使用该方法将事务入队执行
enqueueAction(Runnable runnable)

3、出栈

// 出栈当前Fragment(在当前Fragment所在栈内pop)
pop();

// 出栈某一个Fragment栈内之上的Fragments
popTo(Class fragmentClass/String tag, boolean includeSelf);

// 如果想出栈后,紧接着.beginTransaction()开始一个新事务,请使用下面的方法:
// 防止多事务连续执行的异常
popTo(Class fragmentClass, boolean includeSelf, Runnable afterTransaction)

下面的方法是SupportFragment才有的,操作目标是子Fragment:

popChild();
popToChild(Class fragmentClass, boolean includeSelf);
popToChild(Class fragmentClass, boolean includeSelf, Runnable afterTransaction)
popToChild(Class fragmentClass, boolean includeSelf, Runnable afterTransaction,int popAnim)

4、查找Fragment

// 获取所在栈内的栈顶Fragment
getTopFragment();

// 获取当前Fragment所在栈内的前一个Fragment
getPreFragment();

// 获取所在栈内的某个Fragment
findFragment(Class fragmentClass/String tag);

下面的方法是SupportFragment才有的,从子栈内查找:

getTopChildFragment();
findChildFragment(Class fragmentClass);

6、输入法相关

因为Fragment被出栈时,不会自动隐藏软键盘,以及弹出软键盘有些麻烦,故提供下面2个方法

// 隐藏软键盘 一般用在hide时
hideSoftInput();

// 显示软键盘,调用该方法后,会在onPause时自动隐藏软键盘
showSoftInput(View view);