title: Where to put viewModel Observers in a dialogFragment?
body: <p>For fragments it is <a href="https://medium.com/@BladeCoder/architecture-components-pitfalls-part-1-9300dd969808" rel="noreferrer">advised</a> 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:</p>
  
  <blockquote>
    <p>java.lang.IllegalStateException: Can't access the Fragment View's LifecycleOwner when getView() is null i.e., before onCreateView() or after onDestroyView().</p>
  </blockquote>
  
  <p>From this <a href="https://stackoverflow.com/questions/51614834/what-is-lifecycle-of-dialogfragment">question</a> I read the lifecycle of the dialogFragment at creation is:</p>
  
  <pre><code>onAttach
  onCreate
  onCreateDialog
  onCreateView
  onActivityCreated
  onStart
  onResume
  </code></pre>
  
  <p>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.</p>
  
  <p>This is the code for my observer:</p>
  
  <pre><code>mScheduleViewModel.getTeachers().observe(getViewLifecycleOwner(), new Observer&lt;List&lt;String&gt;&gt;() {
          @Override
          public void onChanged(@Nullable List&lt;String&gt; strings) {
              mStringList = strings;
              aclInputvalue.setThreshold(2);
              aclAdapter.setList(strings);
              aclAdapter.notifyDataSetChanged();
              ....
  }
  </code></pre>
  
  <p>This code pattern works fine in a fragment but not in a dialogFragment. There I have to set the lifecycleOwner to 'this'.</p>
  
  <p>So why do I get the error?</p>