[MVVMbasics] The Life Cycle of MVVMbasics Applications

All blog posts with a title that starts with [MVVMbasics] are related to the MVVMbasics framework. If you are only interested in my regular blog posts and not in MVVMbasics, ignore these. To list all articles covering MVVMbasics, go to http://mvvmbasics.mobilemotion.eu/documentation!

[important]Edited on September 9, 2014 to reflect the changes in the MVVMbasics 2.0 release. The contents may not fully apply to older versions of the MVVMbasics framework.[/important]

The MVVMbasics framework standardizes the life cycle of applications over the three supported platforms (Windows Phone, Windows Store, Desktop). It’s important to distinguish between life cycle events that occur to the application itself, and life cycle events that concern Views which are part of the applicaton. Most of these life cycle events are however passed on to the currently active Viewmodel and can be handled there. This article illustrates which life cycle states an application as well as its Views can reach.

Life cycle of applications

As soon as an application has been started, it can either be terminated by the user, or deactivated (move to the background because another application becomes active). On the Windows Phone and Windows Store platforms, deactivated applications can also be hibernated by the operating system.

The following diagram shows the life cycle of an MVVMbasics application:

Application life cycle events

Those events that are marked in red are automatically forwarded to the currently active Viewmodel and can be handled there – in detail, this applies to the Hibernating, Resuming, Deactivating, and Reactivating events which can be handled by overriding a Viewmodel’s OnNavigatedTo and OnNavigatedFrom methods and filtering for view state types of Hibernated, Awakened, Hidden, and Shown. Details about these events and event handling methods can be found in the article Handling navigation events.

The other events (those that are illustrated in green color) can still be handled within the Application class – however, since Application classes are platform-specific and need to be programmed individually for each target platform anyway, MVVMbasics does not provide standardized ways of listening to these events.

(As for every general rule, there are also exceptions in this case: On the Windows Phone Silverlight platform, the MVVMbasics framework provides the OnLaunching, OnActivated, OnDeactivated, and OnClosing methods that can be overwritten as alternative to registering event handlers for the Launching, Activated, Deactivated, and Closing events.)

Starting:

The Starting event occurs when the App is started by the operating system, for example because the user clicked it’s icon or tile, or because it was requested by another application (e.g., as target of a share contract on the Windows Store platform). This can be handled in the Application class constructor, or by subscribing to the appropriate Launching event in the Application class.

Terminating:

The Terminating event occurs when the App is completely closed, either from C# code or because the user has closed the App’s main View (e.g., by using the hardware back button on a Windows Phone device, or by clicking the [X] button on the main window. Also for this event, there exist appropriate event handler within the Application class.

Deactivating:

The Deactivating events when another application becomes active, but the current one if not closed. This can, for example, happen when the user chooses another running application from the task bar (Desktop platform), or uses the Windows hardware button to leave the App (Windows Phone platform). It might also be triggered by the operating system instead of the user, for example when a text message is received and instantly displayed on a Windows Phone device.

Reactivating:

The Reactivating event occurs when an App that has been deactivated (see the event above) but not completely closed is activated by the user.

Hibernating:

The Hibernating event occurs only on the Windows Phone and Windows Store platforms. It occurs when an App that has already been deactivated is closed by the operating system because of insufficient system resources. This event can not be triggered by the user. This event can be handled directly within the currently active Viewmodel (the Viewmodel that is registered to the currently shown View), by overriding the OnNavigatedFrom method and filtering for view a state type of Hibernated. Details can be found in the article Handling navigation events.

Resuming:

The Resuming event occurs only on the Windows Phone and Windows Store platforms. It occurs when an App that has been hibernated by the operating system (see the event above) because of insufficient system resources is re-activated by the user. This event can be handled directly within the currently active Viewmodel (the Viewmodel that is registered to the currently shown View), by overriding the OnNavigatedTo method and filtering for view a state type of Awakened. Details can be found in the article Handling navigation events.

Life cycle of Views

In contrast to the life cycle events discussed above that occur between two Apps or between the App and the operating system, most life cycle events actually occur within the current App: Those are related to navigation between Views, and they are described in this section.

Basically, navigation within an App means showing and hiding certain Views. Each App contains one Main View, this is the View that is loaded on the application’s startup. From this, other Views can be opened hierarchically, as illustrated in the following picture:

View sequence

On the Windows Phone and Windows Store platforms, these Views actually are pages. If the application navigated to another page, the currently viewed page will be hidden behind the new one because all pages take up the entire screen.

On the Desktop (WPF) platform, Views correlate to windows. A newly opened window might not completely hide the one previously shown, because windows might be smaller than the full screen. However, when navigating to a new window, the currently shown window will always lose focus – it might still be visible on the screen, but it is not possible for the user to interact with. For reasons of compatibility with the Windows Phone and Windows Store platforms, all newly opened windows are modal windows, meaning that they lock the previous window. Concurrent display of two non-modal windows (which would allow the user to interact with both windows at the same time) is not supported by the MVVMbasics framework.

Due to these basic principles of navigation, there exist only four life cycle events:

View life cycle events

All four events can be handled within Viewmodel code and are therefore highlighted in red color.

Showing:

The Showing event occurs when a View is loaded that has never been displayed on the screen, or that had been displayed but was closed again. This event can be handled directly within the View’s attached Viewmodel by overriding the OnNavigatedTo method and filtering for view a state type of Activated.

Closing:

The Closing event occurs when a View is closed an unloaded, but not if another View is loaded that hides the current one. This happens, for example, when the user presses the hardware back button on a Windows Phone device or clicks a Desktop window’s [X] button to close the current windows, or when calling the NavigateBack method from code. It does not happen when calling the NavigateTo method from code, because in this case the current View is not unloaded but remains in background (see the next event). This event can be handled directly within the View’s attached Viewmodel by overriding the CancelNavigatingFrom or OnNavigatedFrom methods and filtering for view a state type of Closed.

Hiding:

The Hiding event occurs when a View is going to be hidden by another View, and is not unloaded but remains in background. This is the case when calling the NavigateTo method from code. This event can be handled directly within the View’s attached Viewmodel by overriding the CancelNavigatingFrom or OnNavigatedFrom methods and filtering for view a state type of Deactivated.

Reactivating:

The Reactivating event occurs when a View that has been hidden and sent to background is displayed again, because the View it has been replaced with is closed. This event can be handled directly within the View’s attached Viewmodel by overriding the OnNavigatedTo method and filtering for view a state type of Reactivated.
Details about handling all four events can be found in the article Handling navigation events.