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?