Use conductor instead Gergely Szőke | Wanari Get rid of Fragments Use conductor instead Gergely Szőke | Wanari
Fragments Problems Support or not Transaction is complicated Backstack management is complicated
Conductor can be the solution View-based Provides wrapper around views Works almost the same as Fragments Easier lifecycle It has 4 main parts: Controller Router ControllerChangeHandler ControllerTransaction
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.
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. It has several methods to work with the backstack (changehandlers also can added): popToRoot popToTag (RouterTransaction can be tagged) popCurrent setBackStack(!)
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. You just have to add push-, and popChangeHandlers to the router
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.
LifeCycle
In action
Activity Works the same as usual You have to create a Router instance providing a container ViewGroup (same as the previous fragmentContainer FrameLayout) Set a root Controller to the router Pass onBackPressed events to the router if it returns false, then leave it to super
Controller You have to implement only the onCreateView method But you have several others like Fragments does Worth to mention: onChangeStarted, onChangeEnded You can handle the the UI during transactions (for example disable, and reenable buttons) You can also handle the optionsMenu from a controller From a controller you can get the activity’s router, to navigate
Nested Controllers Controllers can be nested with ChildRouters It needs a container too There is an optional setting, setPopLastView (default false) If you set it to true, then the child root itself can be popped separately, othervise it pops with the parent controller If you would like to connect two controllers like „onResult” you can setTargetController to the specified controller (the target controller is retained)
About memory things There are two options for controllers, to handle destructive lifecycle events, when to release: ReleaseDetach (default): releases when Detach happens RetainDetach: retains on Detach, but still release on config change Example: If you set on your rootController the RetainDetach mode, then if you navigate back to it, this won’t be recreated
Mosby There is a mosby extension for Conductor, therefore you can use the classic MVP architecture Only one thing to mention, Presenter is instantiated before onAttach, so this is the first place where you can use it
Keep in touch! https://twitter.com/TeamWanari https://www.facebook.com/TeamWanari/
Resources https://github.com/bluelinelabs/Conductor https://github.com/sockeqwe/mosby-conductor