Skip to content
This repository was archived by the owner on Jul 9, 2019. It is now read-only.

Working with FragmentController

Antonio Mindov edited this page Aug 3, 2017 · 22 revisions

After you have instantiated FragmentController you're ready to go using it.

API

  • 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. Pass true if you want the changes to be animated, false otherwise.
  • boolean pop(boolean) - Same as FragmentController#popAsync(boolean) but performs the operation immediately, so there is no need of calling FragmentManager#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. Pass true as a third argument if you want the changes to be animated, false otherwise.
  • boolean popTo(FragmentProvider, boolean, boolean) - Same as FragmentController#popToAsync(FragmentProvider, boolean, boolean) but performs the operation immediately, so there is no need of calling FragmentManager#executePendingTransactions() afterwards. Returns true if something was popped at all, false otherwise.
  • void popToRootAsync() - Pop all back stack entries except the root one.
  • boolean popToRoot() - Same as FragmentController#popToRootAsync() but performs the operation immediately, so there is no need of calling FragmentManager#executePendingTransactions() afterwards. Returns true if there was something to pop, false otherwise.

Examples

Obtaining FragmentController's instance:

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);
        ...
    }
...
}

Pushing a fragment

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);

Popping a fragment

Popping the last entry:

frController.pop(true); // uses FragmentManager#popBackStackImmediate()
frController.popAsync(true); // uses FragmentManager#popBackStack()

Pop to a specific entry:

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)

Pop to the root entry:

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.

Clone this wiki locally