I want to save some data in a Maui Blazor app. To do so i wrote the following code in
App.xaml.sc:
protected override Window CreateWindow(IActivationState activationState)
{
var window = base.CreateWindow(activationState);
window.Destroying += (s, e) =>
{
ShoppingApp.Model.DataManager.saveData();
};
return window;
}
Thanks in advance...
Tried in Windows and Android.
Windows works, Android don't.
I tried that with a breakpoint on the line
ShoppingApp.Model.DataManager.saveData();
On mobile devices, regardless of language used, there is no guarantee of window or app destroy event. The usual approach is to save any time app goes into background ("deactivated").
There is no guarantee app code will ever run again, once app is no longer focused. Mobile OSes reserve the right to kill off an app not in foreground, without warning.
See app lifecycle events, for more info.
(Not referring to services
, foreground or background, which have different rules.)