How do you persist and restore app state across launches?
Short answer
Save lightweight state to Preferences/SecureStorage (or a file/db) in OnDisappearing or the window Stopped event, and restore it in OnAppearing/Resumed or at startup. For navigation state and drafts, serialize what's needed so a cold start can resume the user's context.
Advertisement
AModel answer
Save lightweight state to Preferences/SecureStorage (or a file/db) in OnDisappearing or the window Stopped event, and restore it in OnAppearing/Resumed or at startup. For navigation state and drafts, serialize what's needed so a cold start can resume the user's context.
// Save on stop
Preferences.Default.Set("draft", editor.Text);
// Restore on start
editor.Text = Preferences.Default.Get("draft", string.Empty);Real-world example
A compose screen restores an unfinished message after the OS kills the app in the background.
Likely follow-ups
- Where do you save state? | What survives a cold start?
Related topics