When breaking out a fragment+viewmodel into its own library module, what is your go to strategy for handling a click within that fragment that has an impact on other fragments in the application module (shown at the same time in the same activity)? There are many approaches to this. For example, would you use Koin to provide a click listener? Or maybe declare an abstract viewmodel in the library and then subclass it in the app module providing the click listener that way (and then provide the viewmodel to the library through Koin)?
Mark
03/01/2020, 5:20 AM
For some more context: currently the click is handled in an
activityViewModels
(i.e. using androidx viewmodel framework)
j
Javier
03/01/2020, 4:41 PM
You can use a sharedViewModel
m
Mark
03/02/2020, 3:14 AM
You can also use
activityViewModels
. I know this is going off-koin, but what I ended up doing was splitting the viewmodel into two parts: 1. the internal part scoped at the fragment level, not to be used outside of the library module, and 2. the external part, scoped at the activity level
The activity observes the live data in the “external” viewmodel.
Furthermore, I used this approach for the events: https://proandroiddev.com/navigation-events-in-mvvm-on-android-via-livedata-5c88ef48ee83
j
Javier
03/02/2020, 8:39 AM
You can use two ViewModel, one shared for the common actions/observers and another one private.