Introduction
The lifecycle of a Universal Windows Platform (UWP).
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.
- Public App()
- {
- this.InatializeComponent();
- this.Suspending += OnSuspending;
- this.Resuming += OnResuming;
- }
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.
- public async void OnSuspending(object sender, SuspendingEventArgs e)
- {
- var diferral = e.SuspendingOperation.GetDiferral();
- await SaveLocalData();
-
- ShowToas(" App is Suspending....");
- diferral.complete();
- }
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().
- using(var Session = new ExtendedExecutionSession())
- {
- Session.Reason = ExtendedExecutionReason.SavingData;
- Session.Description = "Uploading Data ";
- Session.Revoked += Session_Revoked;
- var result = await.Session.RequestExtensionAsync();
- await SaveLocalData();
- if(result == ExtendedExecutionResult.Allowed)
- {
- await SaveCloudData();
- }
- }
- private void SessionRevoked(ExtendedExecutionSession Sender, ExtendedRevokedEventArgs args)
- {
-
- CloseCloudConnection();
- }
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.
- private void OnResuming(object sender, object e)
- {
- ReadLocalData();
- ShowToast("App is Resuming...");
- }
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.
- protected override async void OnLaunched(LaunchActivatedEventArgs args)
- {
- Frame rootFrame = Window.Current.Content as Frame;
- if(rootFrame == null)
- {
- rootFrame = new Frame();
-
- SuspensionManager.RegisterFrame(rootFrame, "AppFrame");
- if(args.PreviousExecutionState == ApplicationExecutionState.Terminated)
- {
-
- try
- {
- await SuspensionManager.RestoreAsync();
- }
- catch(SuspensionManagerException)
- {
-
-
- }
- }
-
- Window.Current.Content = rootFrame;
- }
- if(rootFrame.Content == null)
- {
- if(!rootFrame.Navigate(typeof (GroupedItemsPage), "AllGroups"))
- {
- throw new Exception("Failed to create initial page");
- }
- }
-
- Window.Current.Activate();
- }
Summary
In this article, we learned about Windows 10 App LifeCycle.