-
Notifications
You must be signed in to change notification settings - Fork 266
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 stack manager,multistage Nested(StartFragment.java)
This might is the usage eaist to get wrong,and it's really hard to use.We offen need to use fragment for multistage nested, and sometimes we nedd manage fragment by stack, but we offen encounter all kind of exceptions and problems,this library provide powerful api for this.
Now,we need use the parameter containerViewId
in annotation @Puppet, first, the api usage will be introduced and some detail need to notice is next.
1、Push into the stack and show
Rigger.getRigger(this).startFragment(fragment);
This is very easy to use by above code, but which stack is the
fragment
added ? and which container view is the fragment placed in? Now we need use the@Puppet
annotation, there is a parametercontainerViewId
you need to know.
- when
containerViewId
does not equal 0,the fragment to be added will pushs into the local stack, and placed incontainer view
. - when
containerViewId
equal 0, the library will traversal it's host puppet until the valuablecontainerViewId
in puppet class(call ithost
) is found, then the fragment to be added will be pushed into thehost
stack and placed in thehost container view
.
You can use startFragment()
method with the fragment in the stack and choose whether or not to nested them.
2、Use fragment in the some level
For example, there are three classes
AActivity/BFragment/CFragment
, and they ard defined by below code.
@Puppet(containerViewId = R.id.cotainer)
public class AActivity extend AppcompatActivity
@Puppet
public class BFragment extend Fragment
@Puppet
public class CFragment extend Fragment
We use
BFragment
inActivity
,Rigger.getRigger(aActivity).startFragment(bFragment)
,next, we useCFragment
inBFragment
,Rigger.getRigger(bFragment).startFragment(cFragment)
, so which stack iscFragment
pushed in?
Answer: CFragment
and BFragment
is pushed into the AActivity
and placed into the container
in AActivity
. The @Puppet
annotation in BFragment
and CFragment
have not valuable containerViewId
, so the library will traversal it's host puppet until the valuable containerViewId
in puppet class(call it host
) is found.
3、multistage Nested
For example, there are for classes
AActivity
/BFragment
/CFragment
andDFragment
,AActivity
andBFragment
are defined by above code,CFragment
andDFragment
are defined by below code.
@Puppet(containerViewId = R.id.cContainer)
public class CFragment extend Fragment
@Puppet
public class DFragment extend Fragment
We use
BFragment
inActivity
,Rigger.getRigger(aActivity).startFragment(bFragment)
,next, useCFragment
inBFragment
,Rigger.getRigger(bFragment).startFragment(cFragment)
,finally useDFragment
inCFragment
,Rigger.getRigger(cFragment).startFragment(dFragment)
, so which stack is theAFragment/BFragment/CFragment
andDFragment
pushed in?
Answer:BFragment
and CFragment
are pushed ino AActivity
stack, DFragment
is pushed into CFragment
stack.
4、Remove from stack
This library provide support for
Fragment
remove from stack, the default operation is theonBackPressed
method is called, beside, you can useclose()
method to remove a fragment from stack.
The default operation isremove
the pop fragment in stack and show the next pop fragment.
Rigger.getRigger(this).close();
Fragment
is a part of view for the host, so we need choose if the host should be closed as the stack is only contain one fragment, now,the second parameter bondContainerView
in @Puppet
annotation will be used.
-
bondContainerView = true : the
host
that contain only one fragment in stack will befinish
orremove from stack
asonBackPressed()
method is called, and the last pop fragment in stack does not perform the transition animation. -
bondContainerView = false: the
host
that stack is empty willfinish
orremove from stack
and all fragments in stack will perform the transition animation.
5、Print stack fragments
This library provide support to print fragments in stack, and it's easy to read with tree.
Each line is 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