Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin, onActivityResult is deprecated scanning QR code, any ideas

"onActivityResult" is deprecated I don't know how to replace with "StartActivityForResult" any ideas please help, This is my code:

my code works fine but showme warning about deprecated method

this is my function QrScan code:

    private fun scanQRCode() {
    val integrator = IntentIntegrator(this).apply {
        captureActivity = CaptureActivity::class.java
        setOrientationLocked(false)
        setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES)
        setPrompt("Scanning Code")
    }
    integrator.initiateScan()
}

then

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    val result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data)
    if (result != null) {
        if (result.contents == null) Toast.makeText(this, "Operación Cancelada", Toast.LENGTH_LONG).show()
        else {
            resultado = result.contents.toString()
          getlist()
        }
    } else {
        super.onActivityResult(requestCode, resultCode, data)
    }
}

I tried this new way but I can't get it works the new way in Kotlin:

var resultLauncher = registerForActivityResult(StartActivityForResult()) { result ->
    if (result.resultCode == Activity.RESULT_OK) {
        // There are no request codes
        val data: Intent? = result.data
        doSomeOperations()
    }
}

fun openSomeActivityForResult() {
    val intent = Intent(this, SomeActivity::class.java)
    resultLauncher.launch(intent)
}
like image 391
Felipe Camacho Avatar asked Dec 06 '25 20:12

Felipe Camacho


2 Answers

The initiateScan() method calls another method called createScanIntent() which returns the intent necessary for the resultLauncher to work. This way:

private fun scanQRCode() {
    val integrator = IntentIntegrator(this).apply {
        captureActivity = CaptureActivity::class.java
        setOrientationLocked(false)
        setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES)
        setPrompt("Scanning Code")
    }
    resultLauncher.launch(integrator.createScanIntent())
}

Replacing integrator.initiateScan() with resultLauncher.launch (integrator.createScanIntent()) accesses the result of resultLauncher

like image 54
AjRoBSeYeR Avatar answered Dec 10 '25 21:12

AjRoBSeYeR


Using: zxing This is the process:

1- add libraries on gradle:

implementation 'com.journeyapps:zxing-android-embedded:4.3.0'
implementation 'com.google.zxing:core:3.4.1'

2- On your activity when click on button for example call the function:

private void ScanCode() {
        ScanOptions options = new ScanOptions();
        options.setPrompt("Volume up to flash on");
        options.setBeepEnabled(true);
        options.setOrientationLocked(true);
        options.setCaptureActivity(CaptureActivity.class);
        barLauncher.launch(options);
}

3- Display the result:

ActivityResultLauncher<ScanOptions> barLauncher = registerForActivityResult(new ScanContract(),result->{
    if(result.getContents() != null)
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
        builder.setTitle("Result");
        builder.setMessage(result.getContents());
        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();
            }
        }).show();
    }
});
like image 31
Houssin Boulla Avatar answered Dec 10 '25 20:12

Houssin Boulla