I'm replacing deprecated startActivityForResult with new registerForActivityResult.
Everything works fine, except when i try to manage original launcher being destroyed and re-created. In fact, as stated here:
the Activity Result APIs decouple the result callback from the place in your code where you launch the other activity.
as
it is possible (and, in cases of memory-intensive operations such as camera usage, almost certain) that your process and your activity will be destroyed due to low memory.
I'm testing it using Don't keep activities feature in Developer Options, but i'm starting to think it's not the correct way to go.
In the link i posted there is a Testing section, but i didn't undestand why i should use it, as i can test my app simply developing my desired feature.
Here is my code.
In launching activity i wrote, outside every lifecycle method in order to scanQr be created before activity is created:
private val scanQr = registerForActivityResult(
ActivityResultContracts.StartActivityForResult()
) { result: ActivityResult ->
Toast.makeText(this, "Result obtained", Toast.LENGTH_SHORT).show()
}
while second activity is launched here:
private fun openCameraForValidation() {
val intent = Intent(this, ScanQrActivity::class.java)
scanQr.launch(intent)
}
In the second activity, in order to give back a result, i simply execute this.
val dataIntent = Intent()
dataIntent.putExtra(EXTRA_QR_CODE, result ?: "")
setResult(Activity.RESULT_OK, dataIntent)
finish()
I already tried to call
scanQr.unregister()
in first activity OnDestroy, but it didn't make any change.
Could you help me?
Thanks in advance.