User Tools

Site Tools


This is an old revision of the document!

Lifecycle in UWP

Page Lifecycle

After the page constructor finishes, the page goes through the following events:

  • OnNavigatedTo
  • SizeChanged
  • LayoutUpdated - invoked when elements are added or removed from the visual tree or the layout is changed
  • Loaded - occurs only once during the lifetime of a Page

Every FrameworkElement derivative has a Loaded event. The Loaded events occur in a sequence going up the visual tree, ending with the Page derivative.

Example: Handle the Loaded event using an anonymous method:

public MainPage()
    Loaded += (sender, args) =>
        // ..

App Lifecycle

When a user closes an app, the app suspends and after 10 sec it terminates. On the next launch, you can get the PreviousExecutionState from the LaunchActivatedEventArgs.

                                |             |
                                v             |
NotRunning --> Activated --> Running --> Suspended --> Terminated --> NotRunning
                                |             ^

When your app is activated, Windows shows your app's splash screen. While the splash screen is visible, Windows invokes your app's Main method and runs through all the activation steps. One of the last things your app does after initializing is activate its window by calling Windows.UI.Xaml.Window's Activate method. If your app does not call this method within 15 seconds, the OS terminates your app and returns the user to the Start screen. While the OS gives your app 15 seconds to activate its window, your app must actually activate its window within 5 seconds in order to pass Windows Store certification. So you really should design your app to complete its initialization and activate its window within 5 seconds, not 15 seconds. If your app needs more than 5 seconds to initialize, you can implement an extended splash screen.

The Application base class offers a Resuming event. It is raised when an app is resumed and allows your app to refresh its data to show the most current information. If you need to know how long your app was suspended, query the time in the Suspending event and subtract this value from the time obtained in the Resuming event; there might be no need to refresh data if only a small amount of time passed. There is no time restriction placed on your Resuming event's callback method. Many apps do not show real-time data, so they have no need to register with the Resuming event.

notes/uwp/lifecycle.1500469119.txt.gz · Last modified: 2020/08/26 (external edit)