title: Sharing data between fragments using new architecture component ViewModel
body: <p>On Last Google IO, Google released a preview of some new arch components, one of which, ViewModel.</p>
  <p>In the <a href="https://developer.android.com/topic/libraries/architecture/viewmodel.html#sharing_data_between_fragments" rel="noreferrer">docs</a> google shows one of the possible uses for this component:</p>
  <blockquote>
  <p>It is very common that two or more fragments in an activity need to
  communicate with each other. This is never trivial as both fragments
  need to define some interface description, and the owner activity must
  bind the two together. Moreover, both fragments must handle the case
  where the other fragment is not yet created or not visible.</p>
  <p>This common pain point can be addressed by using ViewModel objects.
  Imagine a common case of master-detail fragments, where we have a
  fragment in which the user selects an item from a list and another
  fragment that displays the contents of the selected item.</p>
  <p>These fragments can share a ViewModel using their activity scope to
  handle this communication.</p>
  </blockquote>
  <p>And shows a implementation example:</p>
  <pre><code>public class SharedViewModel extends ViewModel {
      private final SavedStateHandle state;
  
      public SharedViewModel(SavedStateHandle state) {
          this.state = state;
      }
  
      private final MutableLiveData&lt;Item&gt; selected = state.getLiveData(&quot;selected&quot;);
  
      public void select(Item item) {
          selected.setValue(item);
      }
  
      public LiveData&lt;Item&gt; getSelected() {
          return selected;
      }
  }
  
  public class MasterFragment extends Fragment {
      private SharedViewModel model;
  
      @Override
      protected void onViewCreated(View view, Bundle savedInstanceState) {
          super.onViewCreated(view, savedInstanceState);
          model = new ViewModelProvider(getActivity()).get(SharedViewModel.class);
          itemSelector.setOnClickListener(item -&gt; {
              model.select(item);
          });
      }
  }
  
  public class DetailFragment extends Fragment {
      @Override
      protected void onViewCreated(View view, Bundle savedInstanceState) {
          super.onViewCreated(view, savedInstanceState);
          SharedViewModel model = new ViewModelProvider(getActivity()).get(SharedViewModel.class);
          model.getSelected().observe(this, { item -&gt;
             // update UI
          });
      }
  }
  </code></pre>
  <p>I was quite excited about the possibility of not needing those interfaces used for fragments to communicate through the activity.</p>
  <p>But Google's example does not show exactly how would I call the detail fragment from master.</p>
  <p>I'd still have to use <a href="https://developer.android.com/training/basics/fragments/communicating.html" rel="noreferrer">an interface</a> that will be implemented by the activity, which will call fragmentManager.replace(...), or there is another way to do that using the new architecture?</p>