Google has released some new components that are aimed at helping developers create better, more robust and more testable apps. Some of these components include Navigation, ViewModel, LiveData. So how does this fit into the current components such as Activities and Fragments?
First lets quickly go over some architecture components in a nutshell.
Google has released a new component – Navigation. It allows developers to create storyboards that clearly define the User’s flow through the application. With a single line or with implicit links, the NavigationController will take care of everything from handling the fragment transitions, back stack, animations … everything.
To accomplish this a NavHostFragment needs to be defined for the NavigationController to show the new fragments within.
The View Model in Android is pretty simple, it basically defines the structure to allow easy integration with Android. It is the expectation that the developer creates the ViewModel and that Android handles the instantiation and reference handling of them.
A View Model is what encapsulates all the data for views, it is also meant to be the middleman between a View and Model.
Live Data provides streams for observes to be notified on the UI thread when a change has occurred. LiveData automatically observes the lifecycles of views and is able to manage the observers based on their state.
Putting It All Together
With these new components, where does it leave us? MVVM and Single Activity.
MVVM – Model, View, View Model. If you are familiar with this architecture then you’ll be able to see how the View Model component is designed in conjunction with LiveData. If you are new to MVVM, I would strongly recommended doing some research. With these combined, code that added bloat or shouldn’t belong in an Activity / Fragment will clearly have it’s place marked.
Single Activity – With the Navigation component it is intents for use with Fragments, so where does that leave Activities. Well, Google recommends moving to only having one activity serving the sole purpose of providing the entry point to the application leaving the activity as nothing more than a shell. From there use Navigation with Fragments.