
1. Overview
In this article, we will discuss the alternative solution to startActivityForResult deprecated Google sign in.
The startActivityForResult
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 this new Activity Result API directly here which you can use in your project to call google sign in rather than using the deprecated startActivityForResult
.
This API improves code readability, avoids using request code, improves type safety.
2. StartActivityForResult Activity Result API
The Activity Result APIs provide the registerForActivityResult
in place of startActivityForResult +
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 StartActivityForResult
contract is the replacement to the startActivityForResult
. So we will use the StartActivityForResult
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. startActivityForResult deprecated google sign in example
For example, the below registerForActivityResult
method takes the ActivityResultContracts.StartActivityForResult
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.
var resultLauncher = registerForActivityResult( ActivityResultContracts.StartActivityForResult()) { result -> if (result.resultCode == Activity.RESULT_OK) { // parse result and perform action } }
Once you have the ActivityResultLauncher
, you can launch the google sign-in activity using the launch
method:
val gso = GoogleSignInOptions .Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) .requestEmail() .requestProfile() .requestScopes(Scope(DriveScopes.DRIVE)) .build() val mGoogleSignInClient = GoogleSignIn.getClient(this, gso) val signInIntent = mGoogleSignInClient.signInIntent launcher.launch(signInIntent)
Notice that we haven’t passed any request code here as registerForActivityResult
automatically takes care of everything.
4. Conclusion
To sum up, we have discussed the Activity Result API available from androidx 1.2.0 releases as an alternative to the deprecated startActivityForResult google sign in. 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.
One thought on “startActivityForResult deprecated google sign in”