journeyapps / zxing-android-embedded

Barcode scanner library for Android, based on the ZXing decoder
https://journeyapps.com/
Apache License 2.0
5.68k stars 1.26k forks source link

OnActivityResult method is deprecated, what is the alternative? #628

Closed MujahidHi5 closed 2 years ago

MujahidHi5 commented 3 years ago

OnActivityResult method is deprecated, what is the alternative for getting result here?

yannickcharron commented 3 years ago

Hello there I think I got something, you need to swith to registerForActivityResult (https://developer.android.com/training/basics/intents/result)

Declare launcher with the result callback :

private val zxingActivityResultLauncher  = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {

        val intentResult = IntentIntegrator.parseActivityResult(it.resultCode, it.data)

        if(intentResult.contents != null) {
            Toast.makeText(this, intentResult.contents, Toast.LENGTH_LONG).show()
        }
}

And when you need to start the scanner : zxingActivityResultLauncher.launch(intentIntegrator.createScanIntent())

Don't use the initiateScan method which call the old startActivityForResult

This seems to work from my activity, I've not test from a fragment yet. Hope this help you.

msindev commented 3 years ago

Thanks! It is working fine!

phw commented 2 years ago

This should be a part of the documentation :)

rkistner commented 2 years ago

Fixed in v4.3.0 now includes ScannerContract and ScannerOptions which makes this slightly simpler - see the updated readme.