25

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?

Alexander Farber
  • 18,345
  • 68
  • 208
  • 375
KvdLingen
  • 1,006
  • 1
  • 10
  • 19

1 Answers1

7

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.

Carson Holzheimer
  • 2,309
  • 18
  • 33
  • 3
    A minor nitpick - I don't believe DialogFragment ever calls onViewCreated() – Steve M Mar 15 '20 at 19:35
  • `androidx.fragment.app.DialogFragment` does definitely call `onViewCreated`. – isabsent Apr 21 '20 at 16:20
  • 3
    It will call `onViewCreated` if you inflated a Layout in `onCreateView`. If you only provide the Dialog in `onCreateDialog` it will not invoke `onViewCreated`, since `onCreateView` returned `null` before. – dipdipdip May 04 '20 at 18:57