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?
Asked
Active
Viewed 786 times
1

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 Answers
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
-
-
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