Navigation.PopAsync not working after call to ZXing barcode reader

After calling MobileBarcodeScanner - and either scanning a barcode or hitting the android back button from the scanner page, a call to Navigation.PopAsync will not work. The Android Back button will not exit the current page either. The current Page is the top element on the Navigation.NavigationStack - and that does not change.

I am using version 2.4.1 of ZXing and version 4.2.0.778463 of Xamarin.Forms

Is there any way to force the removal of that top page on the stack?

Code to call barcode scanner:

private async Task DoScan()
{
  try
  {
    var options = new MobileBarcodeScanningOptions
    {
      AutoRotate = false,
      UseFrontCameraIfAvailable = false,
      TryHarder = true
    };
    var Scanner = new MobileBarcodeScanner();
    if (Scanner != null)
    { 
      var result = await Scanner.Scan();
      if (result != null)
      {
        ScanResult.Text = result.Text;
      }
    }
  }
  catch (Exception Ex)
  {
    ScanResult.Text = $"Scan Error: {Ex.Message}";
  }
}

Best Answer

  • MichaelOBMichaelOB ✭✭
    Accepted Answer

    The problem was not with the libraries at all.

    I had been trying to test some OnResume functionality which I had thought would only be invoked when the application had gone to sleep. It turns out that after returning from a barcode scan with ZXing - OnResume is called.

    I had: MainPage = new NavigationPage(new StoreLocations()); in OnResume which was causing the problem. I have removed that code and everything now works fine.

    I have also updated to the latest versions of Xamarin.Forms and Xamarin.Essentials.

    Thanks for your help.

Answers

  • MichaelOBMichaelOB Member ✭✭

    Additional note - the Back code:

    private async void BackClicked(object sender,EventArgs e)
    {
      Device.BeginInvokeOnMainThread(async () => await Navigation.PopAsync());
    }
    
  • JohnHardmanJohnHardman GBUniversity mod

    @MichaelOBrien

    I don't use ZXing, but based on previous forum threads, if ScanResult is a UI object, try wrapping ScanResult.Text = result.Text; in Device.BeginInvokeOnMainThread

    Note that I would not expect your BackClicked event handler to need to use Device.BeginInvokeOnMainThread

  • MichaelOBMichaelOB Member ✭✭

    Thanks for the quick reply Michael. I tried your suggestion - and no difference. If the back button is pressed in the ZXing scan page, without scanning, it returns a null. And that is enough to cause the PopAsync to stop working.

    The BackClicked event does not require the Device.BeginInvokeOnMainThread - I put that there just so that there was no question as to whether the PopAsync didn't work due to threading issues.

    The fact is, this used to work until I updated to the latest Xamarin.Forms - from 4.2.0.709249 to 815419. I tried uninstalling 815419 and reinstalling 709249, but that did not make any difference?

  • LeonLuLeonLu Member, Xamarin Team Xamurai

    Can you convert your xamarin forms version back to the 3.1.0.697729? If you have operated it, please open your project folder, delete the bin and obj folders, then re-build your solution.

  • JohnHardmanJohnHardman GBUniversity mod

    @MichaelOB said:
    Thanks for the quick reply Michael. I tried your suggestion - and no difference. If the back button is pressed in the ZXing scan page, without scanning, it returns a null. And that is enough to cause the PopAsync to stop working.

    What returns a null?

  • MichaelOBMichaelOB Member ✭✭

    I will revert all libraries back to the last known version where the ZXing function and the PopAsync was working and see if that resolves the issue. I will post the results of that test.

    The call to MobileBarcodeScanner (ZXing) returns null when the Android back button is used to exit the scanner.

    If anyone knows how I can force the Navigation to pop the current page off of the stack - or reset the Navigation stack and/or navigate to a particular page - please let me know.

  • LeonLuLeonLu Member, Xamarin Team Xamurai

    Ok, waitting for your update.

  • MichaelOBMichaelOB Member ✭✭
    Accepted Answer

    The problem was not with the libraries at all.

    I had been trying to test some OnResume functionality which I had thought would only be invoked when the application had gone to sleep. It turns out that after returning from a barcode scan with ZXing - OnResume is called.

    I had: MainPage = new NavigationPage(new StoreLocations()); in OnResume which was causing the problem. I have removed that code and everything now works fine.

    I have also updated to the latest versions of Xamarin.Forms and Xamarin.Essentials.

    Thanks for your help.

  • LeonLuLeonLu Member, Xamarin Team Xamurai

    Thanks for your sharing, if you have solved your issue, please mark your reply as answer.

Sign In or Register to comment.