EdwardvanRaak / MaterialBarcodeScanner

Easy to use barcode reader for your Android Project (Uses Google Mobile Vision API)
Apache License 2.0
272 stars 113 forks source link

Unwanted Behavior When Building Scanner Inside Fragment #16

Open Maticusfinch opened 8 years ago

Maticusfinch commented 8 years ago

I'm using a TabLayout/ViewPager setup in my app. Inside the first page layout, I have a fragment that contains a FAB that calls the scanner builder. I've tried two different places to call the scanner builder/startScan.
First, I tried creating a new activity dedicated to the scanner that the FAB will call. Everything works great unless the back button on the phone is tapped, in which case it backs out of the camera and to a blank activity. Hitting back at this point finally takes me back to the correct activity. Adding an @override onBackPressed() doesn't fix this problem. I figure it's probably because once the camera is in use, it's no longer in the same activity (that's my theory. Don't know if that's correct or not). Second, I tried setting up a static method within the TabLayout/ViewPager host activity to call from the fragment with the FAB. This has the desired effect the first time, but if I hit the FAB again, it causes this error: org.greenrobot.eventbus.EventBusException: Subscriber class com.edwardvanraak.materialbarcodescanner.MaterialBarcodeScanner already registered to event class com.google.android.gms.vision.barcode.Barcode - I've tried unregistering the EventBus, but nothing that I've tried works.

I'm still learning Android/Java, so I don't know enough to figure this out. I was hoping you might have a solution that I'm missing. Any help would be greatly appreciated!

All that being said, I love this library and want to give you props!

EdwardvanRaak commented 8 years ago

You should definitely try to call the builder inside the original activity that hosts the fragments.

Use an interface callback method to communicate with your activity as described here: https://developer.android.com/training/basics/fragments/communicating.html

That being said, I think I did once test directly creating the builder inside a fragment and experienced no problems...

Maticusfinch commented 8 years ago

Thanks for the quick reply! I'll give that a read and try it out.