-
Notifications
You must be signed in to change notification settings - Fork 265
Fragment usage
Android native fragment operate a series of methods to manage fragment, such as
add
/show
/replace
/remove
, and native Fragment provideFragmentManager
andFragmenmtTransaction
to let us use fragments. But we often encounter all kinds of problems when we are usingFragmentTransaction
. This library can make you use fragment easier.

1、replace method(ReplaceFragment.java)
replace
method is actuallyadd
+show
method, you can make sure onecontainer
only contain oneFragment
when you are using this method.
This method might is the easist method to use fragment, and it is probably one of the easiest ways to get wrong.
Rigger.getRigger(this).replaceFragment(fragment, R.id.fr_content);
Rigger.getRigger(params)
,the params is the Activity/Fragment
class marked by @Puppet.
replaceFragment(@NonNull Fragment fragment, @IdRes int containerViewId)
has two parameters, the first parameter is the fragment to be replaced, the second parameter is the container view id for the fragment to be placed in.
When you are using this method, the fragment is placed in containerView
will be removed, and the fragment to be replaced will be called add
and show
transaction.

2、show method(ShowFragment.java)
show
method has multiple methods, you canadd
multiple fragments or use fragment bytag
use this library.
Type one: Add fragments first and show by fragment object
Fragment fragments[] = new Fragment[4];
Rigger.getRigger(this).addFragment(containerViewId, fragments);
Rigger.getRigger(this).show(fragments[0]);
Type two: Add fragments first and show by fragment tag
Fragment fragments[] = new Fragment[4];
Rigger.getRigger(this).addFragment(containerViewId, fragments);
String tag = Rigger.getRigger(fragment[0]).getFragmentTag();
Rigger.getRigger(this).show(tag);
Type three: Add single fragment and show
Rigger.getRigger(this).addFragment(fragment, containerViewId);
The fragments placed in container view
will be hidden but not remove
, you can show a fragment by fragment object or fragment tag.
hide
method is correspond withshow
method, two methods have samilar usage, but the fragment to be hided must be added first.
Type one: using by object
Rigger.getRigger(this).hideFragment(fragment);
Type two: using by tag
Rigger.getRigger(this).hideFragment(tag);

4、fragment栈管理,多级嵌套(StartFragment.java)
这是我们在使用过程中最容易遇到问题的一个场景,我们常常需要对
Fragment
进行多级嵌套,有时候还需要对其进行栈式管理,但是原生提供的方法在使用过程中会遇到很多问题,本框架对这个场景中可能遇到的一系列问题进行了处理并简化了使用的Api。
要使用本场景下的方法,我们需要用到@Puppet注解中的参数了,我们先对本框架支持的Api进行介绍,然后对其中一些需要注意的地方进行说明。
1、入栈并显示
Rigger.getRigger(this).startFragment(fragment);
通过上面的代码,可以看出,使用的Api很简单,但是此时的
Fragment
是添加到哪里呢?此时我们前面提到的@Puppet
注解就起作用了,在这个注解中有一个参数containerViewId
。
- 当
containerViewId
值不为0时,会添加到本类的栈中,并显示在container view
中。 - 当
containerViewId
为0的时候,会遍历自己所在的持有类,直到找到containerViewId
有效的类(暂称为host
),并添加fragment
到host
的栈中然后显示。
这样设计可以让你在已经添加过的Fragment
中使用本方法,也可以通过自己的需求来选择是否嵌套或者嵌套到哪一层。
2、同级显示场景
假如有三个类,
AActivity/BFragment/CFragment
,三个类的声明如下。
@Puppet(containerViewId = R.id.cotainer)
public class AActivity extend AppcompatActivity
@Puppet
public class BFragment extend Fragment
@Puppet
public class CFragment extend Fragment
我们在
Activity
中对BFragment
进行了操作,Rigger.getRigger(aActivity).startFragment(bFragment)
,我们在BFragment
中又对C进行操作,Rigger.getRigger(bFragment).startFragment(cFragment)
,那么此时的CFragment
是在哪里入栈的呢?
答案:CFragment
和BFragment
都在AActivity
中进行的入栈操作,并显示在AActivity
的container
视图中。因为在BFragment
和CFragment
中@Puppet
的containerViewId
都没有传入参数,所以默认值为0,此时在使用startFragmet
方法的时候会进行遍历操作直到找到有效的container
之后才会进行入栈处理。
3、多层嵌套显示场景
假设我们四个类
AActivity
/BFragment
/CFragment
和DFragment
,AActivity
和BFragment
的声明如上面的代码不变,CFragment
和DFragment
的声明如下:
@Puppet(containerViewId = R.id.cContainer)
public class CFragment extend Fragment
@Puppet
public class DFragment extend Fragment
我们在
Activity
中对BFragment
进行了操作,Rigger.getRigger(aActivity).startFragment(bFragment)
,然后在BFragment
中又对C进行操作,Rigger.getRigger(bFragment).startFragment(cFragment)
,接着在CFragment
中对DFragment
进行操作,Rigger.getRigger(cFragment).startFragment(dFragment)
,那么此时的几个Fragment
分别是在哪里入栈的呢?
答案:BFragment
和CFragment
都是在AActivity
中进行入栈的,DFragment
是在CFragment
进行入栈的。
4、出栈
本框架为
Fragment
出栈提供了一些列支持,默认是按返回键进行出栈,除此之外,提供了close()
方法专门用于出栈操作。
出栈默认的操作是remove
当前的栈顶元素,并对宿主的下一个栈顶元素进行显示。
Rigger.getRigger(this).close();
因为Fragment作为宿主视图的一部分,所以当栈内元素只有一个的时候,我们需要控制是否对宿主进行关闭操作,所以此处需要用到@Puppet
注解中的一个属性,bondContainerView
-
bondContainerView = true :
onBackPressed
被触发的时候,当栈内元素为空或者只剩一个,宿主会跟着出栈或者finish
,此时最后一个栈顶元素不执行转场动画。 -
bondContainerView = false:
onBackPressed
被触发的时候,只有在栈内元素为空,宿主自己才会出栈或者finish
,所有的栈内元素都会执行转场动画。

5、打印栈内元素
本框架提供了栈内成员的实时打印,并使用了树状图来使打印的栈成员更加醒目。
打印的每条记录都为该Fragment的tag。
Rigger.getRigger(this).printStack();
- Installation
- Using start
- Fragment usage
- Custom fragment tag
- Lazy loading
- Transition animations
- Intercept onBackPressed
- startFragmentForResult
- Swipe edge to exit
- How to use in library module