Ghazanfar Khan
Ghazanfar Khan

Reputation: 3718

How to terminate windows phone 8.1 app

I want to exit my app when the user press back button. I have used Hardware Pressed events to navigate between pages. When I navigated to the first page where no Hardware Button press event is used and press back button it goes back to the Menu Screen and not getting terminated as shown in image

enter image description here

Need help.

Upvotes: 1

Views: 2947

Answers (3)

Tushar garg
Tushar garg

Reputation: 92

I also faced the same problem but by using the following piece of code

Application.Current.Exit()

the application shuts down properly.

Upvotes: 6

DividedByZero
DividedByZero

Reputation: 4391

You can use this snippet to make the back button react the same as WP8 on WP8.1. In public App():

#if WINDOWS_PHONE_APP
HardwareButtons.BackPressed += HardwareButtons_BackPressed;
#endif

and in the same scope as App():

#if WINDOWS_PHONE_APP
void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
{
    Frame rootFrame = Window.Current.Content as Frame;

    if (rootFrame != null && rootFrame.CanGoBack)
    {
        e.Handled = true;
        rootFrame.GoBack();
    }
}
#endif

Upvotes: 0

Rob Caplan - MSFT
Rob Caplan - MSFT

Reputation: 21919

This is generally not recommended. Think hard about why you want to do this and if it really makes sense. Barring recovering from a failure it usually doesn't. The expected behavior is for the app to work nicely with the process lifetime code. There is typically no downside to keeping the app suspended, and it is much more efficient to resume from suspension than to restart completely. The user can use the built-in options to explicitly close the app if desired.

That said, you can call Windows.UI.Xaml.Application.Exit .

Upvotes: 4

Related Questions