Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to put viewModel Observers in a dialogFragment?

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?

like image 451
KvdLingen Avatar asked Aug 19 '18 10:08

KvdLingen


1 Answers

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:

  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.

like image 65
Carson Holzheimer Avatar answered Nov 04 '22 10:11

Carson Holzheimer