For fragments it is advised to put liveData observers in the onActivityCreated method. This works fine for fragments, but when I apply this to a dialogFragment I get the following error:
java.lang.IllegalStateException: Can't access the Fragment View's LifecycleOwner when getView() is null i.e., before onCreateView() or after onDestroyView().
From this question I read the lifecycle of the dialogFragment at creation is:
onAttach
onCreate
onCreateDialog
onCreateView
onActivityCreated
onStart
onResume
So putting the observers in onActivityCreated should be fine as it is after onCreateView or onCreateDialog. I use the latter as I use a Alertdialog with my own layout.
This is the code for my observer:
mScheduleViewModel.getTeachers().observe(getViewLifecycleOwner(), new Observer<List<String>>() {
@Override
public void onChanged(@Nullable List<String> strings) {
mStringList = strings;
aclInputvalue.setThreshold(2);
aclAdapter.setList(strings);
aclAdapter.notifyDataSetChanged();
....
}
This code pattern works fine in a fragment but not in a dialogFragment. There I have to set the lifecycleOwner to 'this'.
So why do I get the error?
Update: See Farid's link below, this answer is probably not recommended.
You can use ViewModels
in a DialogFragment
when you are overriding onCreateDialog
by doing this:
onCreateDialog
, store a reference to it as a variable in your DialogFragment
.onCreateView
and simply return your saved custom view.onDestroyView
exactly like this (otherwise Leak Canary reported memory leaks) override fun onDestroyView() {
myView = null
super.onDestroyView()
}
Then, the dialog behaves more like a normal fragment and you can observe your ViewModel
in onCreateView
, onViewCreated
or onActivityCreated
as you please.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With