title: MvvmCross Android - Null Reference for ViewModel when Reloading Fragments
body: <p><strong>Source code</strong> - <a href="https://github.com/benhysell/V.FlyoutTest/" rel="nofollow">https://github.com/benhysell/V.FlyoutTest/</a></p>
  
  <p><strong>Issue</strong> - While intermixing fragments and base views and using the settings on the device - "Developer Options / Don't keep Activities" ViewModels are null if OnDestory() is called and then the activity is brought back into view.  This results in a null reference exception and app crash.</p>
  
  <p><strong>Description</strong>
  I have a slideout/hamburger menu in Android using MvvmCross.  On application start the following steps take place:</p>
  
  <ul>
  <li>HomeViewModel is set to load</li>
  <li>HomeViewModel loads HomeView setting up the slideout/hamburger menu</li>
  <li>Since the HomeView is never really shown, just manages the fragments it calls and shows EnterTimeViewModel, the default view, and the one we really care about.</li>
  <li>At the end of loading HomeView a message is generated using the messenger plugin to "Log In" to the system</li>
  <li><p>Message is received in the EnterTimeViewModel, normally here we'd do some sort of checking to see if we are logged in or not, but in this case we just call ShowViewModel with LoginViewModel to simulate a log in request and prompt for the user.</p></li>
  <li><p>LoginViewModel is not a fragement, just a normal base view.  It is shown to the user</p></li>
  <li>While that is happening, HomeView is being removed from the system because we have set "Developer Options / Don't keep Activities"</li>
  <li>User logs into the system by hitting the log in, in demo this is a no-op and it just closes the LoginView</li>
  <li>The HomeView is then restored, except during that restoration the EnterTimeViewModel is null...when the EnterTime view attempts to access the EnterTimeViewModel a null reference is hit.</li>
  </ul>
  
  <p>The sequence of events with regards to MvvmCross transitions is as follows while loading and unloading HomeView:</p>
  
  <ul>
  <li>OnCreate</li>
  <li>StartActivityForResult</li>
  <li>OnStart</li>
  <li>OnResume</li>
  <li>OnPause</li>
  <li>OnSaveInstanceState</li>
  <li>OnStop</li>
  <li>OnDestroy</li>
  </ul>
  
  <p>Login Screen is shown, after login</p>
  
  <ul>
  <li>OnCreate</li>
  <li>OnStart</li>
  <li>Crash, null reference exception</li>
  </ul>
  
  <p>It feels like a few steps are skipped, resulting in the crash.  Ideas?  Places to go looking?</p>
  
  <p>As a point of reference, the project I based my sliding menu on, <a href="https://github.com/jamesmontemagno/Xam.NavDrawer" rel="nofollow">https://github.com/jamesmontemagno/Xam.NavDrawer</a> exhibits the same issue.</p>
  
  <p><strong>Update</strong>
  Implemented a fix <a href="http://benjaminhysell.com/archive/2014/06/mvvmcross-flyoutnavigation-hamburger-menu-sliding-menu-for-android-null-reference-exception-on-fragment-shown-fix/" rel="nofollow">http://benjaminhysell.com/archive/2014/06/mvvmcross-flyoutnavigation-hamburger-menu-sliding-menu-for-android-null-reference-exception-on-fragment-shown-fix/</a>, tldr - took responsibility to try and save the ViewModels, and worst case re-create them if the fragment is unloaded from memory.</p>