title: getViewLifeCycleOwner() vs 'this' vs this.getActivity() in Androidx Fragments
body: <p>We utilize LiveData's <code>observe</code> method quite a bit in our fragments. A recent release of the androidx fragment sdk is leading to Android Studio marking instances of <code>liveDataObject.observe(this)</code> as incorrect in favor of <code>liveDataObject.observe(getViewLifecycleOwner())</code> .</p>
  
  <blockquote>
    <p>Added a new Lint check that ensures you are using getViewLifecycleOwner() when observing LiveData from onCreateView(), onViewCreated(), or onActivityCreated(). (b/137122478)
    <a href="https://developer.android.com/jetpack/androidx/releases/fragment" rel="noreferrer">https://developer.android.com/jetpack/androidx/releases/fragment</a></p>
  </blockquote>
  
  <p>We are worried about implementing this change because we do not understand how the functionality of <code>getViewLifecycleOwner()</code> compares to that of using <code>this</code>, and whether it will cause a conflict with the use of <code>this</code> or <code>this.getActivity()</code> when setting up the ViewModel in the fragment. </p>
  
  <p>Additionally, we use the Android Navigation component and noticed that when the user navigates to different fragments within the same activity, each fragment's <code>onDestroyView()</code> method is called, <strong>but not <code>onDestroy()</code></strong></p>
  
  <p>Here is an example of our code in <code>onViewCreated()</code></p>
  
  <pre><code> vm.getStemLengths().observe(this, stemLengths -&gt; {
          this.stemLengths = new ArrayList&lt;&gt;(Stream.of(stemLengths).map(stemLength -&gt;
                  new SearchModel(Integer.toString(stemLength.getValue()))).toList());
    });
  </code></pre>
  
  <p>Later, in <code>onDestroyView()</code></p>
  
  <pre><code> vm.getStemLengths().removeObservers(this);
  </code></pre>
  
  <p>At the same time, depending on the fragment, the ViewModel that houses the LiveData is set up with one of the following:</p>
  
  <pre><code> vm = new ViewModelProvider(this.getActivity()).get(PrepareVM.class);
  </code></pre>
  
  <p>To persist the viewmodel across fragments in the activity. </p>
  
  <p>Or:</p>
  
  <pre><code>vm = new ViewModelProvider(this).get(AprobacionVM.class);
  </code></pre>
  
  <p>If the VM does not need to be persisted outside of the current fragment</p>
  
  <p>So to summarize, will changing <code>this</code> to <code>getViewLifeCycleOwner()</code> when observing LiveData objects in fragments' <code>onCreateView()</code> conflict with the ViewModel patterns / Navigation component? Could there be an instance for example where the livedata change ends up triggering an observer from a previous fragment in the same activity that a user navigates away from?</p>
  
  <p>From the documentation of getViewLifeCycleOwner it seems that making this change may allow us to remove the <code>removeObservers()</code> call in each fragment's <code>onDestroyView()</code>. Is that the correct understanding?</p>