// This method saves the application state before entering background state. It fires the event OnBackgroundEntering to collect // state data from the current subscriber and saves it to the local storage. public async Task <bool> SaveStateAsync() { if (OnBackgroundEntering == null) { return(false); } try { SuspensionState suspensionState = new SuspensionState() { SuspensionDate = DateTime.Now }; Type target = OnBackgroundEntering?.Target.GetType(); SuspendAndResumeArgs onBackgroundEnteringArgs = new SuspendAndResumeArgs(suspensionState, target); OnBackgroundEntering?.Invoke(this, onBackgroundEnteringArgs); await ApplicationData.Current.LocalFolder.SaveAsync(StateFilename, onBackgroundEnteringArgs); return(true); } catch (Exception) { // TODO WTS: Save state can fail in rare conditions, please handle exceptions as appropriate to your scenario. return(false); } }
// This method restores application state when the App is launched after termination, it navigates to the stored Page passing the recovered state data. protected override async Task HandleInternalAsync(LaunchActivatedEventArgs args) { SuspendAndResumeArgs saveState = await GetSuspendAndResumeData(); if (saveState?.Target != null && typeof(Page).IsAssignableFrom(saveState.Target)) { NavigationService.Navigate(saveState.Target, saveState.SuspensionState); } }
public async Task RestoreSuspendAndResumeData() { SuspendAndResumeArgs saveState = await GetSuspendAndResumeData(); if (saveState != null) { OnDataRestored?.Invoke(this, saveState); } }
public async Task <SuspendAndResumeArgs> GetSuspendAndResumeData() { SuspendAndResumeArgs saveState = await ApplicationData.Current.LocalFolder.ReadAsync <SuspendAndResumeArgs>(StateFilename); if (saveState?.Target != null && typeof(Page).IsAssignableFrom(saveState.Target)) { return(saveState); } return(null); }