Search code examples
androidandroid-dialogfragmentandroid-architecture-componentsandroid-viewmodel

Should DialogFragments have their own ViewModel or should they use the ViewModel of their containing Fragment?


I am new to android and i am not sure if it is a good practice to let DialogFragments use the ViewModel of their containing Fragment, or should it have a ViewModel of its own.

I am asking this since Google recommends that each Activity/Fragment has its own ViewModel.


Solution

  • I don't see a problem with the dialog having it's own ViewModel. This would fall into the Single Responsibility Principle, where the Fragment only handles what's relevant to it and ditto for the DialogFragment. Try and keep them uncoupled.

    https://en.wikipedia.org/wiki/Single_responsibility_principle