The usage of Fragments in Android apps is highly controversial. While some developers love them, others hate them. In this blog post I will give you a short introduction of how to use Mosby 3.0 (SNAPSHOT) to build MVP base screens and square’s Flow library as navigation stack replacement.
Preface: Usually I use Fragments in my apps and 99% of the time they work well. However, I do understand developers who are advocating against Fragments. We want build the best apps we are able to and if Fragments are a source for errors then this 1% is probably to much.
In this blog post I will show you how to write a little atlas app entirely without Fragments by using Mosby and Flow. Our app has just two screens: A list of countries and a details screen where information about a certain country is displayed. Let’s have a look at a short demo video (please note that this demo app is able to deal with screen orientation changes):
Flow for navigation
The app itself will be a single Activity. As already said, there are basically two screens the user can navigate to:
- CountriesListLayout: Displays a list of countries. The user can click on a country to display details about this country.
- CountryDetailsLayout: Displays details about a certain country like population, currency and some photos.
Dispatcher and Keys
To integrate flow in your activity you have to do the following (kotlin programming language):
Let’s start with an easy thing to spot: We override onBackPressed() to forward the press of android’s back button to Flow.
Just for completeness, R.layout.activity_main is just a FrameLayout “container”:
Next, we will focus on how to configure Flow. To install Flow in our Activity we have to override attachBaseContext(). Why? Well, internally Flow will create a ContextWrapper and we have to use that special context wrapper returned by Flow for our Activity by calling super.attachBaseContext(flowContextWrapper).
Flow is highly customizable which on one hand is great and allows you to be very flexible. On the other hand that means that you have to write some “boilerplate” code. To tell Flow how to navigate in our app we have to define a Dispatcher. The Dispatcher is responsible to “dispatch” changes on Flow’s navigation (history) stack.
Alright, let’s discuss some key aspects of the code shown above: As you see we have to implement Flow’s interface Dispatcher with the method dispatch(). This method will be invoked whenever we use Flow to navigate through the app and we have to specify (manually) how to apply the navigation changes to your view. In the Atlas app we have a “container” (FrameLayout) and whenever we navigate from screen to the next screen (or back to previous screen) we simply remove the current screen from the container and add the new screen. Flow gives us a Traversal object as parameter which contains all the information we need to apply navigation stack changes. We get a “key” destination = traversal.destination.top() from Flow. Every “screen” is identified by a key and you have to make a mapping from key to a android.view.View like we do here:
You may ask yourself “what is a key”? Basically everything (java.lang.Object) can be used as a key for a screen in Flow. It seems to be good practice that “keys” are named with “Screen” suffix. In our atlas app we have two “screens” we can navigate to. Hence we have two “key” classes named CountriesScreen and CountryDetailsScreen. This “key” classes have two responsibilities: First, as already discussed a key maps to an android view, and second the key contains all the required data the screen needs to display. I think that can be compared to fragment arguments. For example the CountryDetailsScreen contains an id (country id) which then is used in the corresponding view to load the data for the given country id.
I guess I know your next question: “Why do we need to implement the parcelable interface?”. I have said earlier that “everything” can be a key and that’s still true. However, at some point (during process death, i.e. activity gets destroyed while in background) Flow has to save your keys persistently in a Bundle (as Parcelable) to be able to restore the navigation stack history after your activity gets restarted (i.e. activity comes in the foreground again). Therefore, we have to provide a KeyParceler to Flow which is responsible to write and read a “key” as parcelable. The easiest way to do so is to make the “key” like CountryDetailsScreen itself parcelable because then our KeyParceler implementation is basically just casting the “key” object like we do in our atlas app:
Please note that the type Any is kotlins equivalent to java.lang.Object
To sum it up, to use Flow in our Activity we have to do:
With .defaultKey(CountriesScreen()) we specify what is our start key / screen:
MVP with Mosby
Alright, so far we have talked about how to setup Flow as navigation stack replacement. We haven’t discussed yet how to build the UI without Fragments. We simply write custom Views by extending from ViewGroups like FrameLayout etc. Furthermore, we want to have a separation of concerns (separation from UI and business logic). This is the point where Mosby comes in.
Mosby is a Model-View-Presenter (MVP) library for Android. For our atlas app we will use Mosby 3.0, which is not released at the time of writing this blog post. However, 3.0.0-SNAPSHOT is available and changes until the final 3.0 release will mainly be “under the hood”. In other words, the API is mostly stable (and compatible to Mosby 2.0).
Screen orientation changes
One of the most loved features of Mosby is that Presenters can survive screen orientation changes. Additionally, Mosby has a tiny companion object to the Presenter and View called ViewState. Typically, in MVP (passive view) the Presenter coordinates the View. So the Presenter tells the View to display a ProgressBar like view.showLoading() while loading data and then the RecyclerView view.showContent() once the data has been loaded. Mosby’s ViewState is some kind of hook sitting between Presenter and View and keeps track of all the methods the presenter has invoked on the view. The idea is that after a screen orientation change we can “apply” the ViewState and invoke the same methods on the View to get back to the UI state as before the screen orientation change.
If you have used Mosby 2.0 before this is nothing new to you. This feature was already available for Activities and Fragments. With Mosby 3.0 this feature is now fully supported for subclasses of android.view.ViewGroup like FrameLayout, RelativeLayout and so on (there was already partial support for that in Mosby 2.0).
Let’s have a look how we have implemented the screen that displays a list of countries:
For more details about Mosby you should read Mosby’s documentation. As you might have already noticed we extend from MvpViewStateFrameLayout which is provided by Mosby. Since Mosby follows the delegation principle is quite easy to make every ViewGroup work Mosby. All you have to do is to implement ViewGroupViewStateDelegateCallback in your custom ViewGroup class and forward “lifecycle events” like onAttachedToWindow() and onDetachedFromWindow() to Mosby’s ViewGroupMvpDelegate. This sounds more complex than it actually is. Let’s have a look at MvpViewStateFrameLayouts source code:
The CountriesPresenter loads a List
As already said, in Mosby the Presenter survives screen orientation changes and the view gets simply attached and detached from the presenter. Mosby is smart enough to detect when the user has been navigated to another screen so that the Presenter will be destroyed permanently. The screen displaying a country details is basically the same and therefore not worth posting the code here again. You can checkout the whole sample code on github.
The aim of this blog post was to demonstrate that we can build an app without Fragments by using Flow for navigation and Mosby for MVP. Both, Flow and Mosby can deal with process deaths (Activity destroyed in background), however, Mosby requires to make the ViewState parcelable (that means that the loaded data, i.e. list of country, has to implement parcelable as well, see documentation). I, personally, think that 95% of app developers just want that their app survive screen orientation changes painlessly and therefore a “simple view state” (not implementing parcelable) is enough (if process death occurs then data will be reloaded entirely).
Depending on your app, Flow may requires you to write a lot of code (especially for Dispatcher). Nevertheless, Flow is really powerful (still in 1.0-alpha) and we haven’t discussed all features of Flow in detail like complex dispatchers with views on top of each other like dialogs or cases where you don’t have a single “container” to display a view but rather something similar as child-fragments (Fragment’s in Fragments) with back button support or Flow services or how does Flow save the instance state (you have to save and restore that manually, see AtlasAppDispatcher). Also “keys” have to override equals() and hashCode() properly. In a nutshell: Flow is not designed for android dev beginners, but the benefit of Flow is huge (if you hate fragments)!
If you are looking for something more simple then Flow you might find Pancakes interesting which is also a navigation stack library but not as powerful as Flow. With Pancakes you would provide a ViewFactory for each “screen” like this:
Mosby should work with Pancakes (and any other navigation stack library) as great as with Flow.
One last thing: people asked me why Mosby doesn’t provide it’s own navigation stack implementation? The reason is that Mosby should and will ever be that tiny little MVP library. Take Mosby as a base scaffold to build your app on top of it with that development stack you like the most (like Flow or Pancakes for navigation or even with Fragments). See the thing is, since Mosby is a library (and not a framework) you can use as much or as little as you want from Mosby’s functionality. Furthermore, implementing a clean navigation stack library is not that easy as it seems or why do you think that the brilliant guys over at square took quite a long time to design and implement Flow?
Please note that for better readability the code shown in this blog post has been cut down a little bit. Check the demo repository for the complete code