ActivityResultContracts.StartIntentSenderForResult

ActivityResultContracts.StartIntentSenderForResult

1. Overview

In this article, we will discuss the new ActivityResultContracts.StartIntentSenderForResult of Activity Result APIs, an alternative to deprecated android.app.Activity.startIntentSenderForResult.

The android.app.Activity.startIntentSenderForResult is deprecated and Android introduced the Activity Result APIs as an alternative in the androidx.activity:activity:1.2.0 and androidx.activity:activity-ktx:1.2.0 releases. So, we will cover the new ActivityContracts.StartIntentSenderForResult in this article which you can use in your project rather than using the deprecated startIntentSenderForResult.

This API improves code readability, avoids using request code, improves type safety.

2. Activity.startIntentSenderForResult deprecated, Alternative

The Activity Result APIs provide the registerForActivityResult in place of startIntentSenderForResult + onActivityResult.

The registerForActivityResult method takes the following two parameters and returns ActivityResultLauncher as output:

1. ActivityResultContracts

A contract defines the type of action or interaction (Intent) requested from an activity. After getting the result from the activity, it then produces the output of a particular type that makes calling activity for result type-safe.

A collection of standard ActivityResultContracts are available for use. Among the contracts available, the StartIntentSenderForResult contract is the replacement to the activity or fragment startIntentSenderForResult. So we will use the StartIntentSenderForResult contract in this article.

2. ActivityResultCallback

The callback is to be called once the activity result is available. Whatever code you plan to write in your old onActivityResult method must be placed here.

2.1. ActivityResultContracts.StartIntentSenderForResult example

For example, the below registerForActivityResult method takes the ActivityResultContracts.StartIntentSenderForResult as contract and then returns the ActivityResultLauncher as output. As we are well aware of the output type, it is type-safe.

You can check whether the result code is OK and handle the response.

private ActivityResultLauncher<IntentSenderRequest> loginResultHandler =
            registerForActivityResult(new ActivityResultContracts
                            .StartIntentSenderForResult(),
                    result -> {
    // handle the result in your own way
});

Once you have the ActivityResultLauncher, perform the following:

  1. Get IntentSender of the PendingIntent. (An IntentSender contains a description of an Intent and target action to perform with it.)
  2. Create an IntentSenderRequest object by using the IntentSenderRequest.Builder that takes the IntentSender as argument
  3. Launch the target activity by passing the IntentSenderRequest to the launch method of the ActivityResultLauncher.
IntentSenderRequest intentSenderRequest = new IntentSenderRequest.Builder(pendingIntent.getIntentSender())
      .build();
loginResultHandler.launch(intentSenderRequest);

Notice that we haven’t passed any request code here as registerForActivityResult automatically takes care of everything.

After the target activity completes, you would get the result in the callback of ActivityResultLauncher.

4. Conclusion

To sum up, we have discussed the ActivityResultContracts.StartIntentSenderForResult available from androidx 1.2.0 releases as an alternative to the deprecated startIntentSenderForResult. You can also refer settings panel article where we are using the Activity Result API for launching the settings panel.

You can also check out our article on Activity Result API for request permissions.

Leave a Reply

Your email address will not be published. Required fields are marked *