dude
dude

Reputation: 592

BackButton event closes the app Windows 10 app

I am currently developing an app for Windows 10. I want to implement a back button event on my app. When I press the back button on the Frame1, the app closes, which as I wanted to do. When I am at the Frame2, and navigate to Frame3, and I press the back button, the app closes itself.

What I want is the back button event on Frame3 make the Frame3 go back to Frame2, and when I press the back button on Frame2, terminates the app.

On my App.xaml.cs

protected override void OnLaunched(LaunchActivatedEventArgs e)
    {

        if (System.Diagnostics.Debugger.IsAttached)
        {
            this.DebugSettings.EnableFrameRateCounter = true;
        }

        Frame rootFrame = Window.Current.Content as Frame;

        if (rootFrame == null)
        {
            rootFrame = new Frame();

            rootFrame.NavigationFailed += OnNavigationFailed;

            if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
            {
                //TODO: Load state from previously suspended application
            }

            // Place the frame in the current Window
            Window.Current.Content = rootFrame;
        }

        if (rootFrame.Content == null)
        {
            // When the navigation stack isn't restored navigate to the first page,
            // configuring the new page by passing required information as a navigation
            // parameter
            rootFrame.Navigate(typeof(Frame1), e.Arguments);
        }
        // Ensure the current window is active
        Window.Current.Activate();
    }

On my Frame1.xaml.cs :

private void 1_BackRequested(object sender, Windows.UI.Core.BackRequestedEventArgs e)
    {
        Frame frame1 = Window.Current.Content as Frame;
        if (frame1 != null)
        {
            e.Handled = true;
            Application.Current.Exit();
        }
    }

On my Frame2.xaml.cs :

private void 2_BackRequested(object sender, Windows.UI.Core.BackRequestedEventArgs e)
    {
        Frame frame2= Window.Current.Content as Frame;
        if (frame2 != null)
        {
            e.Handled = true;
            Application.Current.Exit();
        }
    }

On my Frame3.xaml.cs :

private void 3_BackRequested(object sender, Windows.UI.Core.BackRequestedEventArgs e)
    {
        Frame frame3= Window.Current.Content as Frame;

        if (frame3.CanGoBack)
        {
            e.Handled = true;
            frame3.GoBack();
        } 
    }

Upvotes: 0

Views: 333

Answers (1)

JuniperPhoton
JuniperPhoton

Reputation: 746

That's because the event handlers you added to BackPressed event will fire in FIFO order, so your event handles stack according to your code is that:

When you are in Page1:

1.Close app

When you navigate to Page2:

1.Close app

2.Close app

When you navigate to Page3 from Page2:

1.Close app

2.Close app

3.Goback to the last page

So when you press back button in Page3, the first handler should fired first, which means that it close app instead of going back to the last page.

So how to fix this?

In your each page:

protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            HardwareButtons.BackPressed += HardwareButtons_BackPressed;
        }
protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            HardwareButtons.BackPressed -= HardwareButtons_BackPressed;
        }

That means whenever you leave this page, you unregister the Backpressed event and when you enter a page you register a new one to make it work.

Upvotes: 2

Related Questions