DEV Community

Chingiz Huseynzade
Chingiz Huseynzade

Posted on

Why I choose Conductor over Fragment?

If we go to repository page of Conductor it says that "A small, yet full-featured framework that allows building View-based Android applications". It really does what it says and there are too many pros than cons that I can talk about.

First thing first, it is very easy to integrate and helps you to build single activity application for Android. When Activities first appeared in the documentation of it is written that "a single, focused thing that the user can do". It means that you can do all the things inside one activity and handle all other things with the help of Conductor or Fragment. Moreover, with the single activity you can manage:

- Navigation drawer in all the views
- Toolbar
- Low memory footprint (previous Activities with views exist in the stack, which can cause an out-of-memory exception in endless navigation applications)
Enter fullscreen mode Exit fullscreen mode

So what should turn you into choosing Conductor

Conductor has four main components that you should know first before starting.

1. Controller - The Controller is the View wrapper that will give you all of your lifecycle management features. Think of it as a lighter-weight and more predictable Fragment alternative with an easier to manage lifecycle.

2. Router - A Router implements navigation and backstack handling for Controllers. Router objects are attached to Activity/containing ViewGroup pairs. Routers do not directly render or push Views to the container ViewGroup, but instead, defer this responsibility to the ControllerChangeHandler specified in a given transaction.

3. ControllerChangeHandler - ControllerChangeHandlers are responsible for swapping the View for one Controller to the View of another. They can be useful for performing animations and transitions between Controllers. Several default ControllerChangeHandlers are included.

4. ControllerTransaction - Transactions are used to define data about adding Controllers. RouterControllerTransactions are used to push a Controller to a Router with specified ControllerChangeHandlers, while ChildControllerTransactions are used to add child Controllers.
Enter fullscreen mode Exit fullscreen mode

As we all know when we are using Fragment we need FragmentManager to replace them. Conductor helps you to write minimal code for replacing Views. We should extend all our views to Controller and then if we want to change View calling getRouter().pushController(YourAnotherViewWhichIsController()) which is an important part of Conductor is enough.

The best part of Conductor is that other than Fragment its Controllers lifecycle is simpler to understand.

A lifecycle diagram of Controller

It's navigation and backstack handling is best. Simpler like:

// On MainActivity
@Override
    public void onBackPressed() {
        if (!router.handleBack()) {
            super.onBackPressed();
        }
    }
Enter fullscreen mode Exit fullscreen mode

In conclusion, the thing main thing that turned me into Conductor is it's easy to use structure, backstack handling, and easy lifecycle.

References:
https://github.com/bluelinelabs/Conductor
https://www.polidea.com/blog/Introduction_To_Single_Activity_Applications/

P.S. It's my first post on dev.to and I'm non-native speaker. I can make mistakes. I'm sorry for that.

Discussion (0)