title: Shared ViewModel lifecycle for Android JetPack
body: <p>The documentation <a href="https://developer.android.com/topic/libraries/architecture/viewmodel#sharing" rel="noreferrer">https://developer.android.com/topic/libraries/architecture/viewmodel#sharing</a> describes how we can share the same ViewModel across the different Fragments.</p>
  
  <p>I have some complicated pages in my <strong>single Activity</strong> app with a container and tabs fragments. Each such page has own ViewModel which should be shared with all contained fragments.</p>
  
  <p>The key trick here is to use Activity instead of Fragment to hold my ViewModel.</p>
  
  <p>The problem is that my Activity can have multiple pages with own models and holding the view model for particular page all the time is waste of device resources.</p>
  
  <p>Is there any way to control the life-cycle of ViewModel to destroy it when user leaves the page?</p>
  
  <p>I thought to use the container fragment instead of Activity:</p>
  
  <pre><code>model = ViewModelProviders.of(getPageContainerFragment()).get(SharedViewModel.class);
  </code></pre>
  
  <p>But found this idea not so good because all children fragments should know about the parent which could be not so good.</p>
  
  <p>Is there any alternatives to tackle properly such case?</p>