Search code examples
androidandroid-fragmentsmvvmviewmodel

Does a BottomSheetFragment need ViewModel?


When working with Bottom Sheets and Dialog how to perform operation:

  1. Use a SharedViewModel with fragment that created this bottom sheet?
  2. Don't use a ViewModel at all?
  3. Creating a separate ViewModel for the BottomSheet?
  4. Any other approach that is best practice

Solution

    1. If the bottom sheet/dialog is tightly bound to your "host" fragment (it shares some specific live data), and it is never going to be created from some other fragment, then it's OK to use a shared view model.
    2. If the dialog is dead simple (like one input + 2 buttons), then the viewmodel might not be needed
    3. If the dialog really needs a viewmodel (i.e. it fetches and displays some dynamic data), then a separate viewmodel makes sense