"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)
}
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
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();
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With