Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does MvvmCross create a new ViewModel when back button is pressed

I'm building a Windows 8.1 mobile app with MVVMCross and I noticed some strange behavior when navigating between viewmodels.

I have 2 Viewmodels like so...

public class FirstViewModel : MvxViewModel
{
   public override void Start()
   {
      base.Start();
   }

   public void Init()
   {
      //init code here
   }
   ...
   private void GoForward()
   {
        ShowViewModel<SecondViewModel>();
   }
}

public class SecondViewModel : MvxViewModel
{
   private void GoBack()
   {
      Close(this);
   }
}

When I call Close(this) on SecondViewModel, Mvvmcross calls Start() and Init() on FirstViewModel again, like it was tombstoned.

Why does this happen? Even if I only leave FirstViewModel for a couple seconds, it seems to be tombstoned immediately. How do I stop this from happening? It's frustrating because I now have to maintain the state of FirstViewModel constantly.

like image 684
iupchris10 Avatar asked Feb 12 '26 10:02

iupchris10


1 Answers

You need to write the following line of code in your page constructor. This enables the caching of visited pages.

NavigationCacheMode = NavigationCacheMode.Required;

like image 72
Kristian Vukusic Avatar answered Feb 15 '26 13:02

Kristian Vukusic