title: Pass data between fragment viewmodels
body: <p>Let me just say up front that this is more of a "structural" question, and I'm not asking anybody to write code; I'm just trying to figure out how I should be structuring my application.</p>
  
  <p>I'm using Android's <code>DrawerLayout</code>/<code>NavigationView</code> for my app. This means that <code>MainActivity</code> is the host for all my fragments.</p>
  
  <p>I currently have three fragments (in reality it's many more, but they are more or less exactly like these three fragments, just for different sets of data).</p>
  
  <ul>
  <li>ListFragment</li>
  <li>DetailFragment</li>
  <li>EditFragment (used for both adding and editing)</li>
  </ul>
  
  <p>On my <code>ListFragment</code> I have (surprise!) a list of items. This is a <code>LiveData</code> collection on <code>SharedViewModel</code> (which is tied to <code>MainActivity</code>'s lifecycle). When an item is tapped I pass the event through to <code>MainActivity</code> by means of an interface listener.</p>
  
  <p><code>MainActivity</code> then loads up the <code>DetailFragment</code>. In the same call, I load an instance of <code>SharedViewModel</code> (again tied to <code>MainActivity</code>). I set <code>SharedViewModel.selectedItem</code> to be the tapped item. Then, in <code>DetailFragment</code>'s <code>onCreate</code> function, I get the selected item via <code>ViewModelProviders.of(activity).get(SharedViewModel::class.java).selectedItem</code>.</p>
  
  <p>On the <code>DetailFragment</code>, there's an edit button. This goes through more or less the same routine described above, but loading up the <code>EditFragment</code> instead. When the edited/added item is saved, I add/replace the item in <code>SharedViewModel</code>'s collection through <code>MainActivity</code>'s interface listener.</p>
  
  <p>Obviously this is not optimal for several reasons. For one, it means that I've got at least five large sets of data hanging around for <code>MainActivity</code>'s lifecycle (the entire lifecycle of the app, essentially). Also, <code>MainActivity</code> grows way out of hand as I have to add more and more functions to handle events.</p>
  
  <p>What I <em>want</em> to do is have, for example, my list of items on a <code>ListFragmentViewModel</code> which is tied to <em><code>ListFragment</code>'s lifecycle</em>.  My selected item on a <code>DetailFragmentViewModel</code>, my editing item on an <code>EditFragmentViewModel</code>, etc.</p>
  
  <p>My problem is that I'm not sure how to properly pass the data around in this case. For example, let's say I add a new item in <code>EditFragment</code>. How do I get that into <code>ListFragmentViewModel</code>'s collection of items? <code>ListFragment</code> is in the back-stack, so its viewmodel hangs around and doesn't reload the data when it's navigated back to, since it still has the collection from before. This makes sense and is probably how it <em>should</em> be (after all, who wants to wait for all the data to load when they go to <code>DetailFragment</code> and back to <code>ListFragment</code>?), but it means that I don't get my new item in the collection.</p>
  
  <p>That's just one example, but I'm running into quite a few issues like it (e.g. passing the selected item to <code>DetailFragmentViewModel</code>.)</p>
  
  <p>I'm not quite sure what direction I should even be going here. Can someone more experienced help me out?</p>