title: Multiple LiveData Observers After Popping Fragment
body: <h3>Issue</h3>
  
  <p><em>Summary</em>: Multiple <strong>LiveData</strong> <strong>Observers</strong> are being triggered in a Fragment after navigating to a new Fragment, popping the new Fragment, and returning to the original Fragment.</p>
  
  <p><em>Details</em>: The architecture consists of <strong>MainActivity</strong> that hosts a <strong>HomeFragment</strong> as the <em>start destination</em> in the MainActivity's <strong>navigation graph</strong>. Within HomeFragment is a programmatically inflated <strong>PriceGraphFragment</strong>. The <strong>HomeFragment</strong> is using the navigation component to launch a new child Fragment <strong>ProfileFragment</strong>. On back press the ProfileFragment is popped and the app returns to the HomeFragment hosting the PriceGraphFragment. The PriceGraphFragment is where the Observer is being called multiple times.</p>
  
  <p>I'm logging the hashcode of the HashMap being emitted by the Observer and it is showing 2 unique hashcodes when I go to the profile Fragment, pop the profile Fragment, and return to the price Fragment. This is opposed to the one hashcode seen from the HashMap when I rotate the screen without launching the profile Fragment. </p>
  
  <h3>Implementation</h3>
  
  <ol>
  <li><p>Navigation component to launch new ProfileFragment within HomeFragment.</p>
  
  <p><code>view.setOnClickListener(Navigation.createNavigateOnClickListener(
  R.id.action_homeFragment_to_profileFragment, null))</code></p></li>
  <li><p><strong>ViewModel</strong> creation in Fragment (PriceGraphFragment). The ViewModel has been logged and the data that has multiple Observers only has data initialized in the ViewModel once. </p>
  
  <p><code>override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      priceViewModel = ViewModelProviders.of(this).get(PriceDataViewModel::class.java)
  }</code></p></li>
  <li><p>Listen for data from <strong>ViewModel</strong> in original Fragment (PriceGraphFragment). This is being called multiple times, however it is only expected to have one Observer when the Fragment is loaded.</p>
  
  <p><code>priceViewModel.graphLiveData.observe(
  this, Observer { priceGraphDataMap: HashMap&lt;Exchange, PriceGraphLiveData&gt;? -&gt;
     // This is being called multiple times.
  })</code></p></li>
  </ol>
  
  <h3>Attempted Solutions</h3>
  
  <ol>
  <li>Creating the <strong>Fragment</strong>'s <strong>ViewModel</strong> in the <strong>onCreate()</strong> method.
  <code>priceViewModel = ViewModelProviders.of(this).get(PriceDataViewModel::class.java)</code></li>
  <li><p>Creating the ViewModel using the Fragment's activity and the child Fragment's parent Fragment.<br>
  <code>priceViewModel = ViewModelProviders.of(activity!!).get(PriceDataViewModel::class.java)</code></p>
  
  <p><code>priceViewModel = ViewModelProviders.of(parentFragment!!).get(PriceDataViewModel::class.java)</code></p></li>
  <li>Moving methods that create the Observers to the Fragment's <strong>onCreate()</strong> and <strong>onActivityCreated()</strong> methods.</li>
  <li>Using <code>viewLifecycleOwner</code> instead of <code>this</code> for the <strong>LifecycleOwner</strong> in the method <code>observe(@NonNull LifecycleOwner owner, @NonNull Observer&lt;? super T&gt; observer)</code>.</li>
  <li>Storing the HashMap data that is showing as duplicates in the ViewModel opposed to the Fragment.</li>
  <li>Launching the child Fragment using the <code>ChildFragmentManager</code> and the <code>SupportFragmentManager</code> (on the Activity level).</li>
  </ol>
  
  <h3>Similar Issues and Proposed Solutions</h3>
  
  <ul>
  <li><a href="https://github.com/googlesamples/android-architecture-components/issues/47" rel="noreferrer">https://github.com/googlesamples/android-architecture-components/issues/47</a></li>
  <li><a href="https://medium.com/@BladeCoder/architecture-components-pitfalls-part-1-9300dd969808" rel="noreferrer">https://medium.com/@BladeCoder/architecture-components-pitfalls-part-1-9300dd969808</a></li>
  <li><a href="https://plus.google.com/109072532559844610756/posts/Mn9SpcA5cHz" rel="noreferrer">https://plus.google.com/109072532559844610756/posts/Mn9SpcA5cHz</a></li>
  </ul>
  
  <h3>Next Steps</h3>
  
  <ul>
  <li>Perhaps the issue is related to creating the nested <strong>ChildFragment</strong> (<em>PriceGraphFragment</em>) in the <strong>ParentFragment</strong>'s (<em>HomeFragment</em>) <code>onViewCreated()</code>?</li>
  </ul>
  
  <p><em>ParentFragment</em></p>
  
  <pre><code>override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
      super.onViewCreated(view, savedInstanceState)
      user = viewModel.getCurrentUser()
       if (savedInstanceState == null) {
           fragmentManager
                  ?.beginTransaction()
                  ?.replace(binding.priceDataContainer.id, 
                     PriceGraphFragment.newInstance())
                  ?.commit()
  }
  </code></pre>
  
  <ul>
  <li>Test replacing the LiveData objects with RxJava observables.</li>
  </ul>