1

I'm developing an app with MVVM pattern and architecture components viewmodel with hilt di. Now I want to use viewmodel in dialog but dialog doesn't have by viewModels() delegation to initialize viewmodel. Any idea?

Ali Zarei
  • 3,523
  • 5
  • 33
  • 44
  • Does this answer your question? [Show Dialog from ViewModel in Android MVVM Architecture](https://stackoverflow.com/questions/44146081/show-dialog-from-viewmodel-in-android-mvvm-architecture) – Abanoub Asaad May 11 '21 at 09:54
  • @AbanoubAsaad No, my problem isn't show dialog, I have to use viewmodel in dialog – Ali Zarei May 11 '21 at 09:56

1 Answers1

2

by viewModels() is a Fragment extension function. If you need a ViewModel probably you also need to use a FragmentDialog instead of your current implementation for better separation.

With FragmentDialog by viewModel should work, since FragmentDialog extends Fragment

Róbert Nagy
  • 6,720
  • 26
  • 45
  • So what is proper way of using dialog (not fragmentDialog) in mvvm? – Ali Zarei May 11 '21 at 10:56
  • If it's a simple dialog then `MaterialDialog` is a good option I think -> https://material.io/components/dialogs/android#dialogs If it has logic (needing a viewmodel indicates it has some more complex logic), than use a `FragmentDialog` with a `ViewModel`. Which basically is a `Fragment`, but it's shown as a dialog – Róbert Nagy May 11 '21 at 11:16