Start a Project

Android activity result API

In Android, the user can pass the data from one activity to another. We also get the result from destination activity in order to trigger an operation.

Registering a callback for an Activity Result

When we start an activity as a result, it is possible that it will destroy due to low memory. For this reason, the Activity Result APIs decouple the result callback from the place in your code where you launch the other activity.

When in a ComponentActivity or a  Fragment, the Activity Result APIs provide a registerFoActivityResult() API for registering the result callback. registerFoActivityResult() takes an ActivityResultContract() and an ActivityResultCallBack and returns an ActivityResultLuancher() which you’ll use to launch the other activity.

If there are multiple activity calls, you can call registerForActivityResult() multiple times to register multiple ActivityResultLauncher. We must call registerForActivityResult() in the same order to ensure that the results are delivered to the correct callback.

Launching an activity for result

Receiving an activity result in a separate class

Note:- It is strongly recommended to use the APIs When we have implemented LifecycleOwner. As the LifecycleOwner automatically removes your registered launcher when the Lifecycle is destroyed. And if a LifecycleOwner is not available, each ActivityResultLauncher class allows you to manually call unregister() as an alternative.

 

Reference Links:-

https://developer.android.com/training/basics/intents/result#java

 

 

 

 

 

 

 

 

 

Exit mobile version