onActivityResult 已弃用,如何处理 android(Java) 的 google 登录片段?
onActivityResult deprecated, how to handle google signin in fragment for android(Java)?
@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);
Fragment 中 GoogleSignIn 的 onActivityResult 的替代方案是什么?
在 activity 结果被弃用后,我们使用 someActivityResultLauncher.launch
在Java中:
Intent intent = new Intent(this, Example.class);
someActivityResultLauncher.launch(intent);
在 Kotlin 中:
val intent = Intent(this, Example::class.java)
resultLauncher.launch(intent)
为了获取结果,我们使用 registerForActivityResult
在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();
}
}
});
在 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
}
}
正如here所说,您可以通过调用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());
更新 - 2021 年 11 月 30 日
这里是 new method by google !
其中一种方法是注册ActivityResultContracts,我在我的项目中使用了类似的代码:
//declare ActivityResultLauncher<Intent>
ActivityResultLauncher<Intent> ResultLauncher;
在 onAttach 或 onCreate 中进行赋值
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
}
*/
}
});
要启动 activity 只需使用 .launch (intent)
Intent intent = new Intent(getContext(), SomeClass.class);
ResultLauncher.launch(intent);
您也可以在 中查看类似问题以及如何处理 Kotlin/Java
希望对您有所帮助
似乎没有替代已弃用的 onActivityResult,而是使用新契约。这是一个简洁的片段,用于在包含的 class.
中处理登录 google
https://gist.github.com/wintren/b866232ca01cfbfb4592fe909b989efd
导入 GoogleSignInActivityContract.kt
class 后,Activity 代码将是:
// 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)
}
处理结果的代码片段中有更多内容。
如果有人仍在寻找在片段中请求权限的解决方案:
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()
}
}
您要在何处启动 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)
并且不要忘记 onDestroyView:
googleAuthActivityResultLauncher.unregister()
@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);
Fragment 中 GoogleSignIn 的 onActivityResult 的替代方案是什么?
在 activity 结果被弃用后,我们使用 someActivityResultLauncher.launch
在Java中:
Intent intent = new Intent(this, Example.class);
someActivityResultLauncher.launch(intent);
在 Kotlin 中:
val intent = Intent(this, Example::class.java)
resultLauncher.launch(intent)
为了获取结果,我们使用 registerForActivityResult
在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();
}
}
});
在 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
}
}
正如here所说,您可以通过调用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());
更新 - 2021 年 11 月 30 日
这里是 new method by google !
其中一种方法是注册ActivityResultContracts,我在我的项目中使用了类似的代码:
//declare ActivityResultLauncher<Intent>
ActivityResultLauncher<Intent> ResultLauncher;
在 onAttach 或 onCreate 中进行赋值
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
}
*/
}
});
要启动 activity 只需使用 .launch (intent)
Intent intent = new Intent(getContext(), SomeClass.class);
ResultLauncher.launch(intent);
您也可以在
希望对您有所帮助
似乎没有替代已弃用的 onActivityResult,而是使用新契约。这是一个简洁的片段,用于在包含的 class.
中处理登录 googlehttps://gist.github.com/wintren/b866232ca01cfbfb4592fe909b989efd
导入 GoogleSignInActivityContract.kt
class 后,Activity 代码将是:
// 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)
}
处理结果的代码片段中有更多内容。
如果有人仍在寻找在片段中请求权限的解决方案:
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()
}
}
您要在何处启动 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)
并且不要忘记 onDestroyView:
googleAuthActivityResultLauncher.unregister()