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.
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.
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.
- 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 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.
- 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 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.
- 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();
- }