Search code examples
androidandroid-fragmentsanimationfragmentbarcode-scanner

Android barcodescanner still image with multiple ZXingScannerView


in my app I have a fragment using the dm77/barcodescanner.

I'm working on a shop app, ex: you scan an item -> page of product -> continue shopping -> open another scanner sessione -> etc.

Now, as you can see, because the clinet wants to maintain the back stack, multiple istances of that fragment could be open (but not at the same time).

The first fragment with the scanner works well, the others has a still image, even if I call

scannerView.stopCamera();
scannerView = null;

leaving the first fragment.

NOTE: the other fragment works well if, instead of add we use replace. But that will create problems on the fragment's animations.


Solution

  • After furious debugging, I found how to make all working:

    • When the fragment is visible / in use, create and add programmatically to your layout the ZXingScannerView, starting the camera and preview;
    • When the fragment is not visible / used, remove the ZXingScannerView from your layout;
    • On some android version (like 5.01) be careful to not call startCamera more than one time, otherwise you'll not be able to open it anymore until you restart the app. The error here is caused by dead thread exception.

    Hope to have been enough clear and helpful.