App model

App lifecycle

The app model defines how Universal Windows Platform (UWP) apps are structured, launched, managed, and interact with the Windows operating system. Understanding the app lifecycle is crucial for developing robust and responsive applications.

Key Concepts

App Lifecycle States

A UWP app progresses through the following states:

  1. Not Running: The app is not currently active.
  2. Running: The app is actively being used by the user, with its UI visible and processing user input.
  3. Suspended: The app is in the background, not visible, and its processing is temporarily halted. The system can terminate suspended apps.
  4. Terminated: The app process has been ended by the system. It must be restarted from its initial state.

Handling Lifecycle Events

Your app must handle key lifecycle events to ensure a good user experience. These are typically managed in your app's main entry point or in specific event handlers within your App.xaml.cs (or equivalent for other languages).

Common Events and Methods:

Example (C#):


public partial class App : Application
{
    protected override void OnLaunched(LaunchActivatedEventArgs e)
    {
        // Handle launch logic, e.g., navigate to the main page
        Frame rootFrame = Window.Current.Content as Frame;
        if (rootFrame == null)
        {
            rootFrame = new Frame();
            Window.Current.Content = rootFrame;
        }
        if (e.PrelaunchActivated == false)
        {
            if (rootFrame.Content == null)
            {
                rootFrame.Navigate(typeof(MainPage), e.Arguments);
            }
            Window.Current.Activate();
        }
    }

    private async void OnSuspending(object sender, SuspendingEventArgs e)
    {
        var deferral = e.SuspendingOperation.GetDeferral();
        try
        {
            // Save application state
            // Example: await ApplicationData.Current.LocalSettings.SaveAsync("MySetting", "SomeValue");
        }
        finally
        {
            deferral.Complete();
        }
    }

    private void OnResuming(object sender, object e)
    {
        // Restore application state
        // Example: var mySetting = ApplicationData.Current.LocalSettings.Values["MySetting"];
    }
}
            

App Activation

Activation is how your app starts. It can be initiated by the user clicking a tile, a file, a protocol, or a toast notification. Different activation types require different handling.

Activation Types

Handling Activation Arguments

The LaunchActivatedEventArgs and other activation event arguments provide information about why the app was activated and any data passed to it. You can inspect these arguments to determine the correct action to take.

Property Description
Arguments A string that contains arguments passed to the app when it was activated.
Kind An enumeration (ActivationKind) specifying the type of activation.
PreviousExecutionState The execution state of the app before activation (e.g., Terminated, Suspended).
SplashArgs Arguments for splash screen activation.