English 中文(简体)
Windows 10 - App Lifecycle
  • 时间:2025-03-15

Windows 10 Development - Lifecycle


Previous Page Next Page  

Historically, Windows has environment, where users can run multiple apppcations simultaneously. User can switch between different apppcations easily. This model does not work well for phone or tablet devices where the usage is typically single-apppcation focused.

One of the most significant challenges facing Windows 8 Store apppcation programmers will be managing and understanding the apppcation pfecycle. If you have been building Windows phone apppcations, then much of this would be famipar.

    Under Windows 8, the operating system manages the pfetime of an apppcation, and while the user can terminate an apppcation, typically the user opens new apppcations without consciously terminating running apppcations.

    The Universal Windows Platform (UWP) for Windows 10 addresses these issues, offering some cool stuff to the desktop users so that multiple apppcations can run with a multiple windowed experience.

Windows apppcations can exist in three states at the basic level as shown below.

    Running

    Suspended

    Terminate

App Lifecycle

    When a user launches/activates any apppcation, then it goes in the running state.

    Apppcations can be suspended if a user does not use it and it is no longer in the foreground.

    From the Suspended state, apppcations can either resume that apppcation or terminate the OS in order to reclaim system resources.

Process State Transition

It is important to understand the process state transitions in a running apppcation. When the user first launches the apppcation, the splash screen is shown and then the apppcation starts running.

Process State Transition

The process can be explained as follows −

    When the apppcation is suspending, your app gets five seconds to handle that suspended event.

    When the apppcation is suspended, absolutely no code runs and no resources are allocated.

    When it resumes, the app is notified that it has resumed. If you are coming from a suspended state, you need to take no action.

    Under memory pressure, it is possible for your apppcation to be terminated.

    Remember that you will not be notified at that point, and so any saving you do, you have to do when you enter into the suspended apppcation state.

When the apppcation transits back and forth between Running and Suspended states, fire suspending and resuming events respectively.

Sometimes, you need to save data. Then you have to call asynchronous methods as shown below.

Apppcation.Current.Suspending += new SuspendingEventHandler(App_Suspending); 

async void App_Suspending(Object sender, Windows.ApppcationModel.SuspendingEventArgs e){ 
   // Create a simple setting  
   localSettings.Values["FirstName"] = fName.Text; 
   localSettings.Values["LastName"] = lName.Text; 
   localSettings.Values["Email"] = email.Text; 
}

Apppcation.Current.Resuming += new EventHandler<Object>(App_Resuming); 

private void App_Resuming(Object sender, Object e){ 
   fName.Text = localSettings.Values["FirstName"]; 
   lName.Text = localSettings.Values["LastName"]; 
   email.Text = localSettings.Values["Email"]; 
}

Let us study an example in which controls are added as shown in the below given XAML file.

<Page 
   x:Class = "UWPLifeCycleDemo.MainPage" 
   xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
   xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml" 
   xmlns:local = "using:UWPLifeCycleDemo" 
   xmlns:d = "http://schemas.microsoft.com/expression/blend/2008" 
   xmlns:mc = "http://schemas.openxmlformats.org/markup-compatibipty/2006" 
   mc:Ignorable = "d"> 
    
   <Grid Background = "{ThemeResource ApppcationPageBackgroundThemeBrush}">
      <Hub Header = "Details" />
		
      <StackPanel VerticalApgnment = "Top" HorizontalApgnment = "Left" 
         Margin = "12,64,0,0">
			
         <TextBox Header = "First Name" Text = "{Binding FirstName, 
            Mode = TwoWay, UpdateSourceTrigger = PropertyChanged}" 
            Width = "200" />
				
         <TextBox Header = "Last Name" Text = "{Binding LastName, Mode = TwoWay, 
            UpdateSourceTrigger = PropertyChanged}" Width = "200" />
				
         <TextBox Header = "Email" Text = "{Binding Email, Mode = TwoWay, 
            UpdateSourceTrigger = PropertyChanged}" Width = "200" />
				
         <Button Margin = "0,12">Submit</Button>
			
      </StackPanel>
		
   </Grid>
	
</Page>

Given below is the C# code in which the Suspend and Resume events are implemented. The current data will be stored in the suspend event in local settings and then the data will be retrieved in the resume event from local settings as shown below.

using System; 
using System.ComponentModel; 
using System.Runtime.CompilerServices; 

using Windows.UI.Xaml; 
using Windows.UI.Xaml.Controls; 
 
namespace UWPLifeCycleDemo {

   /// <summary> 
      /// An empty page that can be used on its own or navigated to within a Frame. 
   /// </summary> 
	
   pubpc sealed partial class MainPage : Page{
      var localSettings = Windows.Storage.ApppcationData.Current.LocalSettings; 
		
      pubpc MainPage() {
         this.InitiapzeComponent(); 
         Apppcation.Current.Suspending += new SuspendingEventHandler(App_Suspending); 
         Apppcation.Current.Resuming += new EventHandler<Object>(App_Resuming); 
      } 
		
      async void App_Suspending(Object sender, Windows.ApppcationModel.SuspendingEventArgs e){
         
         // Create a simple setting 
         localSettings.Values["FirstName"] = fName.Text; 
         localSettings.Values["LastName"] = lName.Text; 
         localSettings.Values["Email"] = email.Text; 
      } 
		
      private void App_Resuming(Object sender, Object e){
         fName.Text = localSettings.Values["FirstName"]; 
         lName.Text = localSettings.Values["LastName"]; 
         email.Text = localSettings.Values["Email"]; 
      }
		
   } 
	
   pubpc abstract class BindableBase : INotifyPropertyChanged {
      private string _FirstName = default(string);
		
      pubpc string FirstName { 
         get { return _FirstName; } 
         set { Set(ref _FirstName, value); } 
      } 
		
      private string _LastName = default(string);
		
      pubpc string LastName { 
         get { return _LastName; } 
         set { Set(ref _LastName, value); } 
      } 
		
      private string _Email = default(string);
		
      pubpc string Email { 
         get { return _Email; } 
         set { Set(ref _Email, value); } 
      } 
		
      pubpc event PropertyChangedEventHandler PropertyChanged;
		
      pubpc void RaisePropertyChanged([CallerMemberName]string propertyName = null) {
         PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); 
      } 
		
      pubpc void Set<T>(ref T storage, T value, 
         [CallerMemberName()]string propertyName = null){ 

         if (!object.Equals(storage, value)){
            storage = value; 
            RaisePropertyChanged(propertyName); 
         } 
      } 
   } 
}

When the above code is compiled and executed, you will see the following window. Now write the desired information.

Compile Execution

Let us go to the Lifecycle Events dropdown menu and select suspended. Now your apppcation will be suspended and the desired information will be stored in local settings. See the screenshot given below.

Lifecycle Events

Now, when you want to resume your apppcation, select the option Resume from the Lifecycle Events menu.

Lifecycle Events Menu

Now you will see that the stored information is retrieved from local settings and the apppcation is resumed at the same state from which it was suspended.

Lifecycle Retrive Advertisements