android – Where to put viewModel Observers in a dialogFragment?-ThrowExceptions

Exception or error:

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?

How to solve:

You can use ViewModels in a DialogFragment when you are overriding onCreateDialog by doing this:

  1. When you inflate your custom view in onCreateDialog, store a reference to it as a variable in your DialogFragment.
  2. Override onCreateView and simply return your saved custom view.
  3. Set the view to null in 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.

Leave a Reply

Your email address will not be published. Required fields are marked *