Reputation: 4334
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?
Upvotes: 3
Views: 5656
Reputation: 4334
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.
like below where BaseDialog class extends DialogFragment
Upvotes: 5
Reputation: 167
You can try with this:
Use interface, implement it in fragment so you have callback funtion.
Pass high ordered function, declare it like this in dialog:
var click: (() -> Unit)? = null;
Then you can set it from fragment when you instantiate your dialog.
Use shared view model, for example make view model in your activity and then you can access it from every fragment or dialog like this:
(requireActivity() as MainActivity).viewModel
Like this you can set value in view model variable (liveData often) inside your dialog and observe changes in fragment
Upvotes: 1
Reputation: 80
I think you can pass a high ordered function to dialog and handle it in fragment using viewModel inside.
Upvotes: -1