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.
1 2 3 4 5 6 7 8 9 |
// GetContent creates an ActivityResultLauncher<String> to allow you to pass // in the mime type you'd like to allow the user to select ActivityResultLauncher<String> mGetContent = registerForActivityResult(new GetContent(), new ActivityResultCallback<Uri>() { @Override public void onActivityResult(Uri uri) { // Handle the returned Uri } }); |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
ActivityResultLauncher<String> mGetContent = registerForActivityResult(new GetContent(), new ActivityResultCallback<Uri>() { @Override public void onActivityResult(Uri uri) { // Handle the returned Uri } }); @Override public void onCreate(@Nullable savedInstanceState: Bundle) { // ... Button selectButton = findViewById(R.id.select_button); selectButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { // Pass in the mime type you'd like to allow the user to select // as the input mGetContent.launch("image/*"); } }); } |
Receiving an activity result in a separate class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
class MyLifecycleObserver implements DefaultLifecycleObserver { private final ActivityResultRegistry mRegistry; private ActivityResultLauncher<String> mGetContent; MyLifecycleObserver(@NonNull ActivityResultRegistry registry) { mRegistry = registry; } public void onCreate(@NonNull LifecycleOwner owner) { // ... mGetContent = mRegistry.register(“key”, owner, new GetContent(), new ActivityResultCallback<Uri>() { @Override public void onActivityResult(Uri uri) { // Handle the returned Uri } }); } public void selectImage() { // Open the activity to select an image mGetContent.launch("image/*"); } } class MyFragment extends Fragment { private MyLifecycleObserver mObserver; @Override void onCreate(Bundle savedInstanceState) { // ... mObserver = new MyLifecycleObserver(requireActivity().getActivityResultRegistry()); getLifecycle().addObserver(mObserver); } @Override void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { Button selectButton = findViewById(R.id.select_button); selectButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { mObserver.selectImage(); } }); } } |
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