-1

I'm writing a UWP app to learn the new UWP platform. The main page of my app is a list the user can edit (classic commands like add/edit/delete + reorder). I need to save these items to a local storage or to OneDrive based on the user's choice when the user leaves the app and restore these items when the user returns to the app.

Obviously, the best moments when we can do that is when the main screen of the app is activated or deactivated. The question is: what event will work best for my purpose?

After learning some basics of the UWP platform, I see that the OnSuspending/OnLaunched events provided by the VS2015 UWP Blank App template are not appropriate events to save the list data. What events then? Page's OnNavigatedFrom/OnNavigatedTo I used to use in WP8 Silverlight apps? Or are there other events in the new UWP platform I can use for my needs?

TecMan
  • 2,743
  • 2
  • 30
  • 64
  • 1
    The suspending event should be the one you are looking for, eventually if you have some more work to do, you can extend it (though, [there can be little problems](http://stackoverflow.com/q/36105996/2681948) in some cases). – Romasz Mar 29 '16 at 08:27

2 Answers2

1

I did something like that before.

In my scenario I needed to save the data in OnDrive or save in a local folder (storage folder)

I had the same question and I solved my issue doing something like this:

I used a timer running in the background auto saving all the changes every 2 minutes or 5 minutes.

And I added the possibility to save manually the data with a button just in case if the user wants to save his data manually.

with this technique I prevented to be connected all time to OneDrive or save the data in the local storage everytime if the user changes some property or add another item.

For getting the data I always use OnNavigatedTo to load all the items.

for saving all the data after the user close or leave the app I like to use a Backgroundtask to synchronize all the last saved data in the local storage with Ondrive ( actually I just replace the data)

Best Regards

RicardoPons
  • 1,323
  • 9
  • 14
0

You mention the following:

I see that the OnSuspending/OnLaunched events provided by the VS2015 UWP Blank App template are not appropriate events to save the list data.

What are the things that make you wonder if OnSuspending/OnLaunched aren't the correct way to handle this?

I would sum this:

For save, use Application.Suspending. Use ExtendedExecution if needed.

For load, use Application.OnLaunched. Detect the PreviousExecutionState and restore if needed.

For testing, use "Suspend and shutdown":

VS Suspend and shutdown

Mikael Koskinen
  • 12,306
  • 5
  • 48
  • 63