I'm using Android studio 4.0, coding in Kotlin and I'm still really new at Android development.I need to integrate the Xzing barcode libray. I've copied some code from the tutorial at [https://tutorialwing.com/implement-android-qr-code-scanner-using-zxing-library-in-kotlin/][1]. The tutorial uses the following import statement:
import android.app.Fragment
I've modified a fragment that was generated with the Navigation Drawer Activity template. It uses the androidx fragment import statment:
import androidx.fragment.app.Fragment
The errors it occurs on the following line with the "this" reference:
qrScanIntegrator = IntentIntegrator.forFragment(this)
The error is:
Type mismatch: Required Fragment! found HomeFragment
Changing my import to the older version to the older version is not recommended and if I do I get a lot of other errors.
What can I do to get zxing to work with the androidx fragment? Thanks
The ZXing library is no longer maintained, and that particular class (IntentIntegrator
) only supports android.app.Fragment
which is the deprecated Android framework version of the AndroidX Fragment
class.
You have a few possible options here:
Fork the ZXing library, and change the android.app.Fragment
import to use the androidx.fragment.app.Fragment
version instead.
Delegate the IntentIntegrator
calls to your Activity
instead, and have the results forwarded to your Fragment
.
Simply duplicate the Intent
initialization code from IntentIntegrator
to a helper class of your own. The IntentIntegrator
methods are just convenience methods to build an Intent
and call startActivityForResult()
. You'll still be able to use the IntentIntegrator.parseActivityResult
method to interpret the result that comes back.
I would recommend going with option #3.
As a longer term alternative, you may also want to consider looking into Firebase ML Kit since ZXing is no longer maintained.