In my Xamarin.Forms application, I have 1 page and am setting the view model in the xaml as such:
<ContentPage.BindingContext>
<vm:MainViewModel />
</ContentPage.BindingContext>
The problem is, that when I put the app in the background and then open it again, I have 2 instances of MainViewModel
. I can see with the debugger that a new one is created, but also code from the first one (before I put it in the background) is still being called (from event handlers).
How can I fix this? Is it possible to retain and use the same view model? Or am I forced to have a new one every time I close and reopen the app? If so, how do I make sure the old one is released from memory?
I'm running the app on an Android device.