Skip to content

Fragment usage

JingYeoh edited this page Dec 19, 2017 · 2 revisions

Android native fragment operate a series of methods to manage fragment, such as add/show/replace/remove, and native Fragment provide FragmentManager and FragmenmtTransaction to let us use fragments. But we often encounter all kinds of problems when we are using FragmentTransaction. This library can make you use fragment easier.

1、replace method(ReplaceFragment.java

replace method is actually add + show method, you can make sure one container only contain one Fragment 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 can add multiple fragments or use fragment by tag 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.

3、hide method

hide method is correspond with show 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 parameter containerViewId you need to know.

  • when containerViewId does not equal 0,the fragment to be added will pushs into the local stack, and placed in container view.
  • when containerViewId equal 0, the library will traversal it's host puppet until the valuable containerViewId in puppet class(call it host) is found, then the fragment to be added will be pushed into the host stack and placed in the host 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 in Activity, Rigger.getRigger(aActivity).startFragment(bFragment),next, we use CFragment in BFragment, Rigger.getRigger(bFragment).startFragment(cFragment), so which stack is cFragment 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 and DFragment,AActivity and BFragment are defined by above code,CFragment and DFragment are defined by below code.

@Puppet(containerViewId = R.id.cContainer)
public class CFragment extend Fragment
@Puppet
public class DFragment extend Fragment

We use BFragment in Activity,Rigger.getRigger(aActivity).startFragment(bFragment),next, use CFragment in BFragment,Rigger.getRigger(bFragment).startFragment(cFragment),finally use DFragment in CFragment,Rigger.getRigger(cFragment).startFragment(dFragment), so which stack is the AFragment/BFragment/CFragment and DFragment pushed in?

AnswerBFragment 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 the onBackPressed method is called, beside, you can use close() method to remove a fragment from stack.
The default operation is remove 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 be finish or remove from stack as onBackPressed() method is called, and the last pop fragment in stack does not perform the transition animation.
  • bondContainerView = false: the host that stack is empty will finish or remove 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();