I have two questions about registerForActivityResult call back in android. is it necessary to un register this call back when the activity is going to destroy? or this call back is lifeCycle aware and safe when it is going to use ? and the other question when you use
ActivityResultContracts.StartActivityForResult()
it will bring you a result data. how to manage request codes like the onActivityResult before? assume that you want to start two activities with different request codes for different results and so on. so what is the best approach for handling different request codes from one activity
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result -> if (result.resultCode == Activity.RESULT_OK) {
} }
Q1: is it necessary to unregister this call back when the activity is going to destroy?
Ans: NO
Q2: this call back is lifeCycle aware?
Ans: Yes
Q3: When you use it ActivityResultContracts.StartActivityForResult()?
Ans: it will be used when you creating a custom contract, more info: https://developer.android.com/training/basics/intents/result#custom
Q4:What is the best approach for handling different request codes from one activity?
Ans:
Example: for one activity
->launch the second activity from the first activity:
->in the first activity you must override onActivityResult():
-> in second activity send the result back to the first activity as follow:
Example: for multiple activities
-> if you handling different request codes from one activity:
-> in third activity send the result back
->then your onActivityResult() function in first activity should look like: