Windows Phone 8.1 Universal App Terminates on Navigating Back from Second Page

Windows Phone 8.1 Universal App terminates on navigating back from second page?

This is new to Windows Phone 8.1.

If you create a new Hub Universal App using a VS2013 template, you'll notice a class in Common folder called a NavigationHelper.

This NavigationHelper gives you a hint how to properly react to back button press. So, if you don't want to use the NavigationHelper, here's how to get the old behavior back:

public BlankPage1()
{
this.InitializeComponent();
HardwareButtons.BackPressed += HardwareButtons_BackPressed;
}

void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
{
if (Frame.CanGoBack)
{
e.Handled = true;
Frame.GoBack();
}
}

You can also do it on app level, to avoid having to do it on every page:

public App()
{
this.InitializeComponent();
this.Suspending += this.OnSuspending;

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

#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

WP8.1 app crashes when navigating to another page

Found the solution here and changed my code to

 private async void ItemView_ItemClick(object sender, ItemClickEventArgs e)
{
{

var itemId = ((SampleDataItem)e.ClickedItem).UniqueId;

await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => Frame.Navigate(typeof(Session), itemId));

}
}

I still don't understand this error, but it is now solved.

Navigating back to Main Page in Windows Phone 8.1 app causes exception

Why not just use

    Frame.GoBack();

Btw I hope you do realize, when you AddWorkOut on the second page and add it to a List in MainPage, its just saving it in-memory for that session. The moment app is closed, you will lose the data added.

I will recommend you to save/read the data from a more persistent form like

It is as simple as :

private IsolatedStorageSettings userSettings = IsolatedStorageSettings.ApplicationSettings;

// to add
userSettings.Add("testdata");
userSettings.Save();

//to read
string xx = userSettings[“SomeValue”].ToString();

If you want to scale, consider other ways like a database.

Navigation in Windows Phone 8.1 RT

This problem is caused by subscribing to the BackPressed event from a page, but then not unsubscribing to it. This means that the page instance is kept alive, and the BackPressed event gets handled by multiple event handlers; in this case the Post page and the Comments page are both handling the event and both calling Frame.GoBack().

To solve the problem, unsubscribe from the BackPressed event when leaving the page, i.e. from the OnNavigatedFrom() override.

WP8.1 back button quits the app

Yes, this is the normal behaviour in Windows Phne 8.1 non-SL apps.
Here a workaround to implement in your App.xaml.cs file:

public App()
{
this.InitializeComponent();
HardwareButtons.BackPressed += HardwareButtons_BackPressed;
}

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

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


Related Topics



Leave a reply



Submit