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
- Activation: The process by which an app is launched by the system in response to a user action, a system event, or a file association.
- Lifecycle Events: UWP apps transition through various states (e.g., Running, Suspended, Terminated). The system communicates these transitions via specific events.
- Suspension: When an app is not actively being used, the system can suspend it to free up resources. Suspended apps can be resumed.
- Termination: If memory is scarce, a suspended app may be terminated. Terminated apps must be restarted from their initial state.
- Resuming: When a suspended app is brought back to the foreground, it enters the Resuming state, where it can restore its UI and state.
App Lifecycle States
A UWP app progresses through the following states:
- Not Running: The app is not currently active.
- Running: The app is actively being used by the user, with its UI visible and processing user input.
- Suspended: The app is in the background, not visible, and its processing is temporarily halted. The system can terminate suspended apps.
- 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:
OnLaunched(LaunchActivatedEventArgs e): Called when the app is launched or re-launched. Use this to initialize your app's UI and restore state.OnSuspending(SuspendingEventArgs e): Called when the app is about to be suspended. Save critical data here.OnResuming(object sender, object e): Called when the app is resuming from a suspended state. Restore the app's state.
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
- Launch: The default activation type.
- Share Target: When your app is activated to receive shared content.
- File Activation: When your app is activated to open a specific file type.
- Protocol Activation: When your app is activated by a URI scheme.
- Search Activation: When your app is activated to display search results.
- Toast Notification Activation: When a user clicks on a toast notification.
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. |