简体   繁体   中英

LiveData and MVVM pattern

this is a more "conceptual" question about LiveData and the MVVM pattern on android: I've started coding an app where I have on screen with multiple buttons for different actions, if I understood correctly when a user click one of the buttons, the ViewModel should handle it and then through LiveData update the ui (Activity / fragment) ; the problem is that just for like 4 buttons I need to implement 4 different instances of LiveData to wich the activity has to subscribe, is there any other solution? So far I thought about creating a State data class and updating in when a button is clicked (and then give the instance to the ui to render) is this OK? But again in the activity you have the problem that you have to check for every attribute of the state class and update the ui accordingly, so what would you do in my situation?

What you need is probably a sealed class defining your different states. I think this medium article is perfectly matching what your looking for and it explains very well how to use a sealed class for that purpose. Hope you'll find it useful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM