MvvmCross handles deserialization when an app is restored through a process known as "tombstoning," which involves saving and restoring the state of the ViewModel. Here's a detailed overview of how this works:
Tombstoning Process
When an app is about to be terminated due to low memory or other system constraints, MvvmCross provides a mechanism to save the state of the ViewModel. This is achieved by overriding the `SaveStateToBundle` method in the `MvxViewModel` class. This method allows you to store any necessary data in an `IMvxBundle`, which is then saved by MvvmCross.
csharp
protected override void SaveStateToBundle(IMvxBundle bundle)
{
// Serialize data using JSON or another method and store it in the bundle
var data = JsonConvert.SerializeObject(MyData);
bundle.Data.Add("MyData", data);
}
Restoration Process
When the app is restored, MvvmCross calls the `ReloadFromBundle` method on the ViewModel. This method allows you to deserialize the saved data from the `IMvxBundle` and restore the ViewModel's state.
csharp
protected override void ReloadFromBundle(IMvxBundle bundle)
{
// Deserialize data from the bundle
if (bundle.Data.TryGetValue("MyData", out string data))
{
MyData = JsonConvert.DeserializeObject(data);
}
}
Lifecycle During Restoration
During the restoration process, the ViewModel lifecycle involves several key steps:
1. ReloadFromBundle: This method is called to restore the saved state.
2. Prepare: Only the parameterless version of `Prepare` is called. It is the developer's responsibility to save and restore any used parameters using `SaveStateToBundle` and `ReloadFromBundle`.
3. Initialize: The `Initialize` method is called as normal to set up the ViewModel.
4. InitializeTask: This watches the initialization task and fires property changes as needed.
Additional Lifecycle Hooks
Starting with MvvmCross 5.0, ViewModels are coupled to the lifecycle of the view, providing additional methods like `ViewCreated`, `ViewAppearing`, `ViewAppeared`, `ViewDisappearing`, `ViewDisappeared`, and `ViewDestroy`. These methods allow for more refined control over the ViewModel's state during view lifecycle events[1][3].
Handling Orientation Changes
It's worth noting that during orientation changes, the ViewModel is not recreated unless tombstoning occurs. If you need to perform specific actions during orientation changes, you can handle these in the view's `OnConfigurationChanged` method[2].
Overall, MvvmCross provides a robust mechanism for saving and restoring ViewModel state, ensuring that apps can recover gracefully from tombstoning or other interruptions.
Citations:[1] https://www.mvvmcross.com/documentation/fundamentals/viewmodel-lifecycle
[2] https://stackoverflow.com/questions/49322704/mvvmcross-view-model-lifecycle-restore-not-working
[3] https://www.mvvmcross.com/mvvmcross-5-release/
[4] https://www.mvvmcross.com/documentation/advanced/customizing-using-App-and-Setup
[5] https://www.mvvmcross.com/documentation/getting-started/mvvmcross-overview
[6] https://stackoverflow.com/questions/37407568/mvvmcross-viewmodel-exception-when-app-returns-from-background
[7] https://schrist.hashnode.dev/automatically-persist-and-restore-application-state-in-reactiveui
[8] https://learn.microsoft.com/en-gb/answers/questions/1304403/xamarin-forms-app-is-always-restarting-when-resumi
[9] https://www.mvvmcross.com/mvvmcross-6.2.0-release/