-
Notifications
You must be signed in to change notification settings - Fork 1
Working with FragmentController
After you have instantiated FragmentController you're ready to go using it.
-
PushBody.Builder pushBody()
- If you want to show a certain fragment, this is where you start from. This method returns a PushBody.Builder object that will help you with adding the fragment. Once you're done, the transaction will be committed by the FragmentController, without you bothering about it.
This is basically a wrapper of a single FragmentTransaction. -
void push(PushBody)
- If you have built a PushBody object but haven't pushed it yet, this is how you can do that. This method will commit the transaction wrapped in the body. -
void popAsync(boolean)
- Pop a single entry from the child FragmentManager's back stack. If there is only the root fragment in the back stack, it will be considered as there is nothing to pop. Passtrue
if you want the changes to be animated,false
otherwise. -
boolean pop(boolean)
- Same asFragmentController#popAsync(boolean)
but performs the operation immediately, so there is no need of callingFragmentManager#executePendingTransactions()
afterwards. Returns true if something was popped at all, false otherwise. -
void popToAsync(FragmentProvider, boolean, boolean)
- Pop to the first back stack entry with the same name as the tag from the given FragmentProvider. Whether that entry will be popped itself, depends on what you pass as a second argument. If this is the entry that corresponds to the root fragment, the inclusive parameter will be ignored. Passtrue
as a third argument if you want the changes to be animated,false
otherwise. -
boolean popTo(FragmentProvider, boolean, boolean)
- Same asFragmentController#popToAsync(FragmentProvider, boolean, boolean)
but performs the operation immediately, so there is no need of callingFragmentManager#executePendingTransactions()
afterwards. Returnstrue
if something was popped at all,false
otherwise. -
void popToRootAsync()
- Pop all back stack entries except the root one. -
boolean popToRoot()
- Same asFragmentController#popToRootAsync()
but performs the operation immediately, so there is no need of callingFragmentManager#executePendingTransactions()
afterwards. Returnstrue
if there was something to pop,false
otherwise.
If you're in an Activity and you just added the FagmentController, then you already have its instance. But if you're in the root fragment of the controller you can get the parent fragment and cast it ot FragmentController (as mentioned in the javadoc, FragmentController is also a fragment). In order to hide the ugly casting, we give you a handy util method:
public class RootFragment extends Fragment {
...
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
...
final FragmentController frController = FragmentUtil.getFragmentController(this);
...
}
...
}
FragmentController works with PushBodies. They wrap a single FragmentTransaction. For more information click here.
// without animation
frController.pushBody()
.addToBackStack(true)
.fragment(fragmentProvider)
.push();
// with default animation
frController.pushBody()
.addToBackStack(true)
.withAnimation(true)
.fragment(fragmentProvider)
.push();
// with custom animation
frController.pushBody()
.addToBackStack(true)
.customAnimation()
.enter(R.anim.slide_in_right)
.exit(R.anim.slide_out_left)
.popEnter(android.R.anim.slide_in_left)
.popExit(android.R.anim.slide_out_right)
.end()
.fragment(fragmentProvider)
.push();
final PushBody pushBody =
FragmentUtil.getFragmentController(this).pushBody()
.addToBackStack(true)
.withAnimation(true)
.immediate(true) // Fragment controller will call FragmentManager#executePendingTransactions()
.fragment(fragmentProvider)
.build();
fragmentController.push(pushBody);
frController.pop(true); // uses FragmentManager#popBackStackImmediate()
frController.popAsync(true); // uses FragmentManager#popBackStack()
frController.popTo("SerachFragment", false, true); // pop all entries till you get to the one with the given tag.
frController.popTo("SerachFragment", true, true); // pop all entries till you get to the one with the given tag including itself.
FragmentController#popTo(String,boolean,boolean)
uses FragmentManager#popBackStackImmediate(String,int)
FragmentController#popToAsync(String,boolean,boolean)
uses FragmentManager#popBackStack(String,int)
frController.popToRoot(); // calls FragmentManager#executePendingTransactions() afterwards.
frController.popToRootAsync(); // doesn't call FragmentManager#executePendingTransactions()
Click here to see how to disable the animation when you're popping a transaction that has one.