android-mvvm icon indicating copy to clipboard operation
android-mvvm copied to clipboard

Support for fragments

Open manas-chaudhari opened this issue 8 years ago • 6 comments
trafficstars

MvvmActivity allows for easy setup of activities. A similar MvvmFragment can be added to allow easy setup of fragments.

manas-chaudhari avatar Dec 20 '16 03:12 manas-chaudhari

@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 avatar Jan 21 '17 06:01 vinayppatil1989

@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 avatar Jan 31 '17 11:01 manas-chaudhari

@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.

vinayppatil1989 avatar Jan 31 '17 12:01 vinayppatil1989

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.

manas-chaudhari avatar Jan 31 '17 13:01 manas-chaudhari

Got that. But before I take that approach, what problems do you see in using R file in VM's?

vinayppatil1989 avatar Jan 31 '17 16:01 vinayppatil1989

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.

manas-chaudhari avatar Feb 06 '17 10:02 manas-chaudhari