0

I am developing a barcode scanner, my MainPage has a CaptureElement in full screen. I have two other pages that I navigate to: HystoricalPage and AboutPage. To stop the async when I navigate from MainPage to one of the others pages I use the Dispose(); method in my OnNavigatedFrom. Like this:

protected override void OnNavigatedFrom(NavigationEventArgs e)
{
     _mediaCapture.Dispose();
}

With this I navigate between pages just fine, sometimes when I go back to may MainPage the CaptureElement does not reinicialize but that is not my issue right now. My issue is the following:

When I am in one of my pages and press the home button and after come back to my app; press the search button and after come back to my app; when I hold the back button and after come back to my app. In all this situations my application crashes. I read that it might be fixed using the resuming and the suspending events, but I don't know how to add those events to my application. If anyone could help me I'd apreciate. Thanks!

Augusto Accorsi
  • 317
  • 5
  • 21
  • 1
    Why does your app crash? Enable break on Common Runtime Language exceptions in Debugging -> Exceptions – xmashallax May 26 '15 at 13:43
  • 1
    When you debug your app, it's not being *Suspended* and thus *OnNavigatedFrom* is not fired (via NavigationHelper) (btw: there may be also other exception cennected with suspension when run in release mode). I think you should find some help with this posts: [one](http://stackoverflow.com/a/29048823/2681948), [two](http://stackoverflow.com/q/28113120/2681948), [three](http://stackoverflow.com/q/23971899/2681948) and [app-lifeccyle](https://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh464925.aspx). – Romasz May 26 '15 at 13:53

3 Answers3

0

I was facing similar issues with a camera app when the start button was pressed. The way I resolved it was:

protected override void OnNavigatedFrom(NavigationEventArgs e)
{
    App.Current.Resuming -= App_resuming;
    App.Current.Suspending -= App_suspending;
    _mediaCapture.Dispose();
}

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    App.Current.Resuming += App_resuming;
    App.Current.Suspending += App_suspending;
}

private void App_suspending(object sender, SuspendingEventArgs args)
{
    _mediaCapture.Dispose();
}

private void App_resuming(object sender, object e)
{
    //reinitialize _mediaCapture object
}

As said by romasz above, you won't be able to test this accurately on debug mode, even though you can use lifecycle events to suspend and resume while debugging, I've found that some classes (for example the BackgroundMediaPlayer) behave in a strange way, so it's always best to test the app by deploying it.

Ali250
  • 652
  • 1
  • 5
  • 19
  • The best way to test suspending (apart from final test on release mode) is to test it in [debug mode manually](http://stackoverflow.com/a/24103734/2681948). Also as Rob Caplan mentioned in his answer better would be to subscribe to *Window's* events - in some cases suspension is not triggered. – Romasz May 26 '15 at 20:38
  • Like I said, sometimes even that does not work properly. When I was using `BackgroundMediaPlayer`, suspending the app manually resulted in the background media player task being canceled as well, something which doesn't (and shouldn't) happen when running in release mode. It worked just fine when testing my camera app though. – Ali250 May 26 '15 at 20:41
0

The navigation events fire only when navigating within the app itself. They don't trigger when the user switches out of the app by pressing the windows key. To handle that case you can dispose the MediaCapture in the CoreWindow.Activated event when the WindowActivationState is Deactivated.

Rob Caplan - MSFT
  • 21,714
  • 3
  • 32
  • 54
  • Just a remark - *OnNavigatedFrom* will be called when the user switches out with windows key - if OP have used *Frame.GetNavigationState* method (which is used by *SuspensionManager* template). Also one must watch out for *CoreWindow.Activated* event as it will also be fired for example when *MessageDialog* shows up. – Romasz May 26 '15 at 20:51
0

I also came across those issues and managed to resolve it by using the .dispose on media capture. please take a look on my comments on here for additional info: https://github.com/phonegap/phonegap-plugin-barcodescanner/issues/166

k7hulu
  • 25
  • 4