Windows 10 App LifeCycle

Application Life-cycle

                                                   Figure 1:Application Life-cycle

Application Life-cycle:
  • Windows store application 3 different states are available. Application is always in any one of the following state:

    • Running
    • Not Running
    • Suspended
  • While moving from one state to another "Event" is called.
  • When you start an application from the storage device, it activates and comes to the Running state.
  • When you launch another application, the system pushes the first application to the background and waits for few seconds to see whether the user intends to immediately switch back to that application.
  • After that time interval, the background application moves to Suspended state. During the transition process, the developer can store all unsaved information to the disk (which is local data used for future reference).
  • Suspend state is considered like an app is going to terminate.
  • OS itself has to decide what to do when the OS Memory is full or running less power mode. In this type of situation, the OS sends the termination message to the application.

    AppCycle
                                              Figure 2: App Life Cycle

  • When the user launches another application, the already running application enters into the background and pushes to the suspended state as mentioned above.
  • The similar process goes on until the system encounters low memory. This ensures that the user can quickly load the suspended application from the memory.
  • When the system notices a low memory while launching an application, it quickly checks for the unused application which was in suspended mode for long time. It terminates that application first and launches the new application in the foreground.
  • If the user wants to resume a suspended application, during that time, the running application moves to the suspended state and that app moves out from the suspension to the running state.

    Application Lifetime
                                  Figure 3: Resources Usage in each stage of 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 to the low power state, the application suspends itself. While pushing the application to the background, the system waits for few seconds to make sure that the user doesn’t want to switch back to the application immediately. After the few seconds 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 the 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. }  

 

Up Next
    Ebook Download
    View all
    Learn
    View all