@Override **//depricated**
public void onActivityResult(int requestCode, int resultCode, @Nullable @org.jetbrains.annotations.Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == REQUEST_OAUTH_REQUEST_CODE) {
insertAndVerifySession();
}
}
}
GoogleSignIn.requestPermissions(
fragment,
REQUEST_OAUTH_REQUEST_CODE,
GoogleSignIn.getLastSignedInAccount(context),
fitnessOptions);
What's the alternative of onActivityResult for GoogleSignIn in Fragment?
As here said, you can get sign in intent by calling getSignInIntent
ActivityResultLauncher<Intent> exampleActivityResult= registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
new ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult result) {
if (result.getResultCode() == Activity.RESULT_OK) {
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(result.getData());
handleSignInResult(task);
}
}
});
//call
exampleActivityResult.launch(mGoogleSignInClient.getSignInIntent());
Update - 30-11-2021
here is the new method by google !
One of the ways is to register ActivityResultContracts, i used similar code in my project:
//declare ActivityResultLauncher<Intent>
ActivityResultLauncher<Intent> ResultLauncher;
inside onAttach or onCreate do the assignment
ResultLauncher = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
result -> {
if (result.getResultCode() == Activity.RESULT_OK) {
...//do stuff on result here
/*
There are no request codes
You can put request code in extra and Unpack it as your string/int e.t.c
Intent data = result.getData();
assert data != null;
String req_code = data.getStringExtra("reqCode");
if(req_code.equal(REQUEST_OAUTH_REQUEST_CODE){
...do stuff
}
*/
}
});
to launch activity just use .launch (intent)
Intent intent = new Intent(getContext(), SomeClass.class);
ResultLauncher.launch(intent);
Also you can check this answer for similar question and how to handle it in Kotlin/Java
Hopes it helps
After on activity result is deprecated we use someActivityResultLauncher.launch
In Java:
Intent intent = new Intent(this, Example.class);
someActivityResultLauncher.launch(intent);
In Kotlin:
val intent = Intent(this, Example::class.java)
resultLauncher.launch(intent)
For fetching results we use registerForActivityResult
In Java:
ActivityResultLauncher<Intent> exampleActivityResult= registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
new ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult result) {
// There are no request codes in this method
if (result.getResultCode() == Activity.RESULT_OK) {
Intent data = result.getData();
}
}
});
In Kotlin:
var exampleActivityResult= registerForActivityResult(StartActivityForResult()) { result ->
if (result.resultCode == Activity.RESULT_OK) {
// There are no request codes in this method
val data: Intent? = result.data
}
}
There doesn't seem to be replacement for the deprecated onActivityResult, but to use the new contracts. Here's a neat snippet to handle sign in to google in a contained class.
https://gist.github.com/wintren/b866232ca01cfbfb4592fe909b989efd
After importing the GoogleSignInActivityContract.kt
class the Activity code would be:
// Before onCreate
private val googleSignInRequest = registerForActivityResult(
GoogleSignInActivityContract(),
::onGoogleSignInResult
)
private val googleSignInOptions: GoogleSignInOptions
get() =
GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.GooglePlayServiceKey_WebClient))
.requestEmail()
.requestProfile()
.build()
fun triggerFunction {
googleSignInRequest.launch(googleSignInOptions)
}
There's more inte the snippet for handling the result.
If anyone is still looking for a solution to request permission in a fragment:
private lateinit var googleAuthActivityResultLauncher: ActivityResultLauncher<Intent>
private val fitnessOptions: FitnessOptions by lazy {
FitnessOptions.builder()
.addDataType(DataType.TYPE_ACTIVITY_SEGMENT, FitnessOptions.ACCESS_WRITE)
.build()
}
onAttach:
googleAuthActivityResultLauncher = registerForActivityResult(StartActivityForResult()) { result ->
val task = GoogleSignIn.getSignedInAccountFromIntent(result.data)
task.addOnCompleteListener {
if (task.isSuccessful) handleRequest()
}
}
where you want to launch the intent:
val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.addExtension(fitnessOptions)
.build()
val googleSignInClient = GoogleSignIn.getClient(requireContext(), gso)
val signInIntent = googleSignInClient.signInIntent
googleAuthActivityResultLauncher.launch(signInIntent)
and do not forget onDestroyView:
googleAuthActivityResultLauncher.unregister()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.