Android Data Binding and MVVM Overview
Android development is growing steadily, consequently more and more apps are being published in Google Play. This has led to the creation of many libraries and frameworks to make development easier. With the new abilities of Android SDK and external libraries, code writing no longer has to follow the same old routine. After the release […]
Technologies
Android development is growing steadily, consequently more and more apps are being published in Google Play. This has led to the creation of many libraries and frameworks to make development easier. With the new abilities of Android SDK and external libraries, code writing no longer has to follow the same old routine. After the release of ButterKnife developers no longer have to bind instances of views to fields manually, however there are still a lot of fields to manage view instances. Now there is a powerful instrument called Data Binding Library. This library provides multiple benefits, one of them is the Model-View-ViewModel (MVVM) architectural pattern. For example, if an application development follows Model-View-Presenter (MVP) design pattern, each change of presenter state should be tracked and a view update should be called. On the other hand, when a user interacts with the view, or the view state is changed we should update a presenter. Without Data Binding we have to write code just to keep MVP architecture clean. MVVM makes this easier, because the only thing you should do in order to update the view is simply to change a view model state. Also you can use @BindAdapter to make two way communication and create custom bindings. Having a viewmodel, allows easy testing without using mocks because all the code is platform independent so you can check a viewmodel state and be sure that everything on UI looks fine.
I would recommend using a data binding library if you need to do the following:
-
Quickly implement view representation, even if you don’t follow MVP or MVVM design patterns;
-
Easily test view model business logic and be sure that UI looks fine;
-
Read a lot of communication code that presents in MVP pattern. Binding adapters allow you to do that;
-
Have a separate place with additional logic for representing model data to a view. You can put it in a viewmodel and bind it to a view the way you want
Concerns
Using a data binding library also enables you to write some business logic in layout xml file. This isn’t always recommended since you have a view model that performs the same task. When part of the business logic is in the layout, it is hard to support multiple layouts. Also, it decreases code testability and readability. It’s important to be careful with viewmodel because if Activity or Fragment is destroyed, views binding to viewmodel wouldn’t restore their state, because viewmodel instances were lost. You should always save and restore viewmodel instances to avoid this.
Conclusion
Android Data Binding library gives the ability to use MVVM design pattern making code less complex and saving time in application development. By using this library, we can forget about Butter Knife fields binding and reuse our view bindings, that could be simple or complex. MVVM provides better testability.
To learn more, contact the professionals at Swan Software Solutions today. Our experts are prepared to discuss what software development, including Android Data Binding and MVVM, can do for your business.