Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How does tombstoning work in MvvmCross


How does tombstoning work in MvvmCross


Tombstoning in MvvmCross refers to the process of saving and restoring the state of a ViewModel when an application is suspended or terminated due to low memory conditions, a common scenario in mobile platforms like Windows Phone and Android. MvvmCross provides mechanisms to handle this process effectively.

Construction and Lifecycle

MvvmCross ViewModels are instantiated using an IoC container, which injects dependencies into the ViewModel. The lifecycle of a ViewModel in MvvmCross includes several key methods:

- Construction: The ViewModel is created with dependencies injected via the IoC container.
- Init(): Initializes the ViewModel with navigation parameters.
- ReloadState(): Restores the state of the ViewModel after tombstoning.
- Start(): Called when initialization and rehydration are complete[3].

Saving State

When an application is tombstoned, MvvmCross calls the `SaveStateToBundle` method on the ViewModel. This method allows you to save the state of your ViewModel by storing data in an `IMvxBundle`. It is advisable to use JSON serialization for storing complex data[1].

Restoring State

After the application is reactivated, MvvmCross attempts to restore the ViewModel's state. However, the exact method for restoring state can vary depending on the platform and version of MvvmCross. In general, the `ReloadState` method is used to rehydrate the ViewModel with the saved state[3].

Platform-Specific Considerations

Each platform supported by MvvmCross handles tombstoning differently:

- Windows Phone: Tombstoning is handled through page events like `OnNavigatedTo` and `OnNavigatedFrom`. MvvmCross provides mechanisms to save and restore state during these events, but it may require additional setup to ensure proper state restoration[4][5].
- Android: MvvmCross handles tombstoning by saving and restoring the ViewModel state when the application is paused or resumed. However, issues can arise if the backstack is cleared or if navigation parameters are not properly saved[6].

Challenges and Solutions

Handling tombstoning in MvvmCross can be challenging, especially when navigating with parameters. It is crucial to save navigation parameters along with the ViewModel state to ensure correct restoration of the application's state[6][9]. Additionally, using helper libraries or custom solutions can simplify the process of saving and restoring state across different platforms[5][8].

In summary, MvvmCross provides a robust framework for handling tombstoning by saving and restoring ViewModel state. However, it requires careful implementation and consideration of platform-specific nuances to ensure seamless application recovery.

Citations:
[1] https://www.mvvmcross.com/documentation/fundamentals/viewmodel-lifecycle
[2] https://www.mvvmcross.com/mvvmcross-51-release/
[3] https://stackoverflow.com/questions/29372480/mvvmcross-viewmodel-life-cycle
[4] https://stackoverflow.com/questions/16023307/mvvmcross-and-tombstoning
[5] https://www.kenneth-truyers.net/2013/03/13/tombstoning-made-easy-in-windows-phone-mvvm/
[6] https://github.com/MvvmCross/MvvmCross/issues/834
[7] https://github.com/MvvmCross/MvvmCross/blob/develop/CHANGELOG.md
[8] https://jesseliberty.com/2011/04/08/tombstoning-and-mvvm/
[9] https://github.com/MvvmCross/MvvmCross/issues/2458
[10] https://www.mvvmcross.com/mvvmcross-56-release/