Search code examples
androidmvvmviewdialogviewmodel

Can a Dialog have a view model in android?


I need to do an API call from a Dialog. Do I need to go back to the fragment for doing so, or is there any way to refer to the fragment view model?


Solution

  • Yes, it is possible and I was able to do that because class DialogFragment extends Fragment. So I added a view model just like any other fragment.

    enter image description here

    like below where BaseDialog class extends DialogFragment

    enter image description here