android-mvvm
android-mvvm copied to clipboard
Support for fragments
MvvmActivity allows for easy setup of activities.
A similar MvvmFragment can be added to allow easy setup of fragments.
@manas-chaudhari How do we add/remove fragments from VM? Since adding fragments requires a TAG, LayoutId and FragmentManager a service won't do since that would require a separate method for every fragment(because layoutID can't be passed from VM or can we?). Your thoughts please.
@vinayppatil1989 You're right. VM shouldn't be aware about layoutId. You can pass an interface to the View similar to Navigator in the sample and perform the fragment transaction in its implementation. That way, the fragment logic stays in your view layer (parent fragment/activity)
@manas-chaudhari That way, wouldn't the interface impl get bloated up with a method for every fragment. It gets worse with fragment removing logic.
Yes, it would. But, I think its necessary if you have different parameters for different navigations. If you have common arguments for navigation, you can pass the information as a parameter. For example, you could define a Page enum (or an IntDef) and pass it to the navigation method. The activity/fragment can store a mapping from Page -> layoutIds.
If you use Kotlin, then Page can be a sealed class and you can pass arguments through a common method.
Got that. But before I take that approach, what problems do you see in using R file in VM's?
Problem is that that would couple your layout to the ViewModel. Hence, you won't be able to use the same ViewModel with another layout.