Introduction

The lifecycle of a Universal Windows Platform (UWP).
Application Life-cycle
Figure 1:Application Life-cycle

Application Life-cycle

Application Launch

When the process is not running and the user activates that process, the application comes to Launch state. This could be either because the application was never launched or it was suspended but removed from the memory. When an application is launched directly from the storage device, it displays a splash screen to the user. During that time, it completes all main tasks of the application to load it completely. Once all the primary tasks are done, it closes the Splash screen and activates the application main UI.
  1. Public App()
  2. {
  3. this.InatializeComponent();
  4. this.Suspending += OnSuspending;
  5. this.Resuming += OnResuming;
  6. }

Application Suspend

When a user moves an application to the background or the device enters the low power state, the application suspends itself. While pushing the application to the background, the system waits for a few seconds to make sure that the user doesn’t want to switch back to the application immediately. After a few seconds of time interval, the system automatically moves the application to the suspended state. During the transition, you may want to store the unsaved data to the storage device.
Based on your available memory, the system wants to keep a number of suspended apps into the memory to make sure the user can quickly switch back to the application. As I mentioned above, you can’t close an application directly. The app always resides in memory as long as your system has available memory to execute. If your available memory is low, the system automatically removes the unused application from memory.
  1. public async void OnSuspending(object sender, SuspendingEventArgs e)
  2. {
  3. var diferral = e.SuspendingOperation.GetDiferral();
  4. await SaveLocalData();
  5. //await SaveClodData();...........// Use in ExtendedExecution explained below.
  6. ShowToas(" App is Suspending....");
  7. diferral.complete();
  8. }
Remember that, the system never fires notification during termination and hence you have to handle your unsaved data during the Suspend state itself within the time limit of 5 seconds.
If you need extra time to save the state then you request for it using ExtendedExecution().
  1. using(var Session = new ExtendedExecutionSession())
  2. {
  3. Session.Reason = ExtendedExecutionReason.SavingData; // You can select another reason instead of SavingData
  4. Session.Description = "Uploading Data "; // Use to inform user
  5. Session.Revoked += Session_Revoked;
  6. var result = await.Session.RequestExtensionAsync();
  7. await SaveLocalData(); // Save Local data i.e. Normal operation first.
  8. if(result == ExtendedExecutionResult.Allowed)
  9. {
  10. await SaveCloudData();
  11. }
  12. }
  13. private void SessionRevoked(ExtendedExecutionSession Sender, ExtendedRevokedEventArgs args)
  14. {
  15. // clean our data model
  16. CloseCloudConnection();
  17. }

Application Resume

You can resume a suspended application from memory and bring it to the foreground thread. During the state transition, it loads the application data if you already saved during the suspension state.
  1. private void OnResuming(object sender, object e)
  2. {
  3. ReadLocalData();
  4. ShowToast("App is Resuming...");
  5. }
There is no event associated with app termination. We can check whether the app got terminated earlier through the activation event. Code snippet for verifying the previous state of the application.
  1. protected override async void OnLaunched(LaunchActivatedEventArgs args)
  2. {
  3. Frame rootFrame = Window.Current.Content as Frame;
  4. if(rootFrame == null)
  5. {
  6. rootFrame = new Frame();
  7. //Associate the frame with a SuspensionManager key
  8. SuspensionManager.RegisterFrame(rootFrame, "AppFrame");
  9. if(args.PreviousExecutionState == ApplicationExecutionState.Terminated)
  10. {
  11. // Restore the saved session state only when appropriate
  12. try
  13. {
  14. await SuspensionManager.RestoreAsync();
  15. }
  16. catch(SuspensionManagerException)
  17. {
  18. //Something went wrong restoring state.
  19. //Assume there is no state and continue
  20. }
  21. }
  22. // Place the frame in the current Window
  23. Window.Current.Content = rootFrame;
  24. }
  25. if(rootFrame.Content == null)
  26. {
  27. if(!rootFrame.Navigate(typeof (GroupedItemsPage), "AllGroups"))
  28. {
  29. throw new Exception("Failed to create initial page");
  30. }
  31. }
  32. // Ensure the current window is active
  33. Window.Current.Activate();
  34. }

Summary

In this article, we learned about Windows 10 App LifeCycle.