title: Creating singleton view model instance in MvvmCross Android
body: <p>I'm using <code>MvvmCross</code> to develop my Android app. In that I want to have a Singleton instance of particular <code>ViewModel</code>. For that I tried to implement in similar way as mentioned on <a href="https://stackoverflow.com/questions/16723078/mvvmcross-does-showviewmodel-always-construct-new-instances/28725607#28725607">this link</a> </p>
  
  <p>Following is my code in my App.cs</p>
  
  <pre><code>public class App : Cirrious.MvvmCross.ViewModels.MvxApplication
  {
      public override void Initialize()
      {
          CreatableTypes()
              .EndingWith("Service")
              .AsInterfaces()
              .RegisterAsLazySingleton();
  
          RegisterAppStart(new AppStart());
      }
  
      protected override Cirrious.MvvmCross.ViewModels.IMvxViewModelLocator CreateDefaultViewModelLocator()
      {
          return MyViewModelLocator ();
      }
  }
  
  public class MyViewModelLocator : MvxDefaultViewModelLocator
  {
      public MyViewModelLocator()
      {
      }
  
      public override IMvxViewModel Load(System.Type viewModelType, IMvxBundle parameterValues, IMvxBundle savedState)
      {
          if (viewModelType.GetType() == typeof(CartViewModel))
          {
              var cache = Mvx.Resolve&lt;IMvxMultipleViewModelCache&gt;();
              var cachedViewModel = cache.GetAndClear(viewModelType);
  
              if (cachedViewModel == null)
                  cachedViewModel = base.Load(viewModelType, parameterValues, savedState);
  
              cache.Cache(cachedViewModel);
  
              return cachedViewModel;
          }
          else
          {
              return base.Load(viewModelType, parameterValues, savedState);
          }
      }
  }
  </code></pre>
  
  <p>But somehow with this implementation in place, it is not able to get the ViewModel from cache and cache.GetAndClear call always return null inside <code>ViewModelLocator</code> implementation. Apart from this place, I don't call cache from any other place from my code so wonder how come it gets removed from cache. </p>
  
  <p>Also, I was wondering, why I simply cannot make use of IOC (i.e. <code>Mvx.RegisterSingleton&lt;ViewModelName&gt;()</code> and <code>Mvx.Resolve&lt;ViewModelName&gt;()</code>) to get singleton instance instead of cache? Internal implementation of MvvmCross uses cache and not IOC.</p>