×
Namespaces

Variants
Actions
(Difference between revisions)

How to terminate a Windows Phone app

From Nokia Developer Wiki
Jump to: navigation, search
influencer (Talk | contribs)
(Influencer - Another method)
influencer (Talk | contribs)
(Influencer -)
Line 73: Line 73:
  
 
If you are completely aware of the current app state's back stack, you can end the app by removing entries from the back stack with
 
If you are completely aware of the current app state's back stack, you can end the app by removing entries from the back stack with
[http://msdn.microsoft.com/en-us/library/system.windows.navigation.navigationservice.removebackentry.aspx NavigationService.RemoveBackEntry Method]. If you have removed all entries but the last a call to [http://msdn.microsoft.com/en-us/library/system.windows.navigation.navigationservice.goback.aspx NavigationService.GoBack Method] terminates the app.
+
[http://msdn.microsoft.com/en-us/library/system.windows.navigation.navigationservice.removebackentry.aspx NavigationService.RemoveBackEntry Method]. If you have removed all entries '''but the last''' a call to [http://msdn.microsoft.com/en-us/library/system.windows.navigation.navigationservice.goback.aspx NavigationService.GoBack Method] terminates the app.
 +
Be sure to leave one entry on the back stack, the one that brings you from the first page back to the previously used app in order to avoid an exception from the GoBack method.

Revision as of 09:57, 25 September 2013

This article explains how to terminate a Windows Phone app programmatically.

SignpostIcon XAML 40.png
WP Metro Icon WP8.png
SignpostIcon WP7 70px.png
Article Metadata
Tested with
SDK: Windows Phone 8.0 SDK
Devices(s): Nokia Lumia 920, WP7.1 Emulator
CompatibilityArticle
Created: Kunal Chowdhury (04 Sep 2013)
Last edited: influencer (25 Sep 2013)

Contents

Introduction

The Windows Phone app lifecyle is such that apps are typically terminated by the user pressing the back button, or pushed into a dormant state (and perhaps later tombstoned) if the user activates another app from the start screen.

Even though it is not common practice, at times, one may want to terminate the Application programmatically. There are a number of cases where this is necessary - for example where the app has had an exception and you cannot guarantee that it is in a "safe" state you may wish to terminate after logging the issue. Other use cases might include security applications where you don't want the user to be able to enter further data/continue using the app.

Windows Phone 8

To terminate the application completely, you just have to call the following API:

Application.Current.Terminate();

This will terminate the running application completely. The place to call this API may vary depending upon your requirement.

Tip.pngTip: Make sure to save important data before calling this API. No lifecycle events will be fired following termination


Windows Phone 7

The Game::Exit() method (from Microsoft.Xna.Framework namespace) can be used to programmatically exit an app on WP7. This can be used even in non-XNA apps.

Warning.pngWarning: An alternative is to raise an exception which your app does not handle. This approach is not recommended because app crashes affect your App Store ranking, and because it may lead to certification issues.


Cross-version method for terminating an app on WP7 and WP8

Unfortunately neither approach compiles on both versions, so to get the same code to work on both platforms requires reflection. The code below compiles and works on both Windows Phone 7 and Windows Phone 8.

Tip.pngTip: If you already know that your app will only be targeting WP8 it is better to use Application.Current.Terminate() directly.

        private static void Terminate()
{
if (Environment.OSVersion.Version.Major < 8)//try to load XNA assemblies (only working on WP7)
{
System.Reflection.Assembly asmb = System.Reflection.Assembly.Load("Microsoft.Xna.Framework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=842cf8be1de50553");
asmb = System.Reflection.Assembly.Load("Microsoft.Xna.Framework.Game, Version=4.0.0.0, Culture=neutral, PublicKeyToken=842cf8be1de50553");
Type type = asmb.GetType("Microsoft.Xna.Framework.Game");
object obj = type.GetConstructor(new Type[] { }).Invoke(new object[] { });
type.GetMethod("Exit").Invoke(obj, new object[] { });
}
else// => WP8
{
Type type = Application.Current.GetType();
type.GetMethod("Terminate").Invoke(Application.Current, new object[] { });
}
}

Another way that works on WP7 and WP8 under certain conditions

If you are completely aware of the current app state's back stack, you can end the app by removing entries from the back stack with NavigationService.RemoveBackEntry Method. If you have removed all entries but the last a call to NavigationService.GoBack Method terminates the app. Be sure to leave one entry on the back stack, the one that brings you from the first page back to the previously used app in order to avoid an exception from the GoBack method.

544 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×