title: When is the viewmodel onCleared called
body: <p>Are ViewModels independent of activity/fragment lifecycles or just their configuration changes. When will they cease to exist and the subsequent onCleared() method called. 
  Can the viewModel be shared with another Activity ?</p>
  
  <p>A situation:</p>
  
  <pre><code>Activity1+viewModel1---&gt;(rotation)---&gt;Activity1+viewModel1
  ---&gt;(launch Intent)---&gt;Activity2+viewModel1
  </code></pre>
  
  <p>is this sharing possible and is it a good practice.</p>
  
  <p>Also, since the app lifecycle callbacks, onPause->onStop->onDestroy is same for both </p>
  
  <p>1.activity rotating and </p>
  
  <p>2.when an Activity ends, </p>
  
  <p>how is a ViewModel figuring out internally the right time to call onCleared and finally end its lifecycle. </p>
  
  <hr>
  
  <p><strong>Findings</strong>:</p>
  
  <p><em>the ViewModel uses a holderFragment internally to hold an instance of the activity and uses the setRetainInstance method like fragments to account for configuration changes.</em></p>
  
  <p><a href="https://android.jlelse.eu/deep-dive-inside-of-androids-viewmodel-architecture-components-e6756dc0bb11" rel="noreferrer">Source: <strong>dive-inside-of-androids-viewmodel-architecture-components</strong></a></p>
  
  <p><a href="https://i.stack.imgur.com/rESXi.png" rel="noreferrer"><img src="https://i.stack.imgur.com/rESXi.png" alt="enter image description here"></a></p>