使用AppAuth-Android会创建一个独立的任务
我正在尝试使用AppAuth-Android来对Fitbit的 Web API进行身份验证。
我调用AuthorizationService.performAuthorizationRequest() 来显示Fitbit的 OAuth界面:
val authEndpoint = Uri.parse(config.FITBIT_AUTH_URL)
val tokenEndpoint = Uri.parse(config.FITBIT_TOKEN_URL)
val redirectUrl = Uri.parse(config.FITBIT_REDIRECT_URL)
val authConfig = AuthorizationServiceConfiguration(authEndpoint, tokenEndpoint)
val request = AuthorizationRequest.Builder(authConfig, config.FITBIT_CLIENT_ID, ResponseTypeValues.CODE, redirectUrl)
.setPrompt("consent")
.setScope(config.FITBIT_SCOPES.joinToString((" ")))
.build()
val completionIntent = PendingIntent.getActivity(
this, 0,
Intent(this, AuthorizeFitbitActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_NEW_TASK
},
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_MUTABLE
)
val cancelIntent = PendingIntent.getActivity(
this, 1,
Intent(this, AuthorizeFitbitActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_NEW_TASK
putExtra(EXTRA_AUTH_CANCELLED, true)
},
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_MUTABLE
)
this.fitbitAuthService.performAuthorizationRequest(request, completionIntent, cancelIntent)
界面确实显示出来了,但在一个不同的任务中打开,因此当用户完成并关闭授权的网页界面时,他们现在已经拥有我的应用的两个实例。
我该如何让网页界面保持在应用的同一个任务中,或者在用户关闭后将该任务关闭,以便回到只有一个实例的状态?
解决方案
我不太明白为什么,但去掉启动Activity的 task affinity就解决了这个问题。(调用上述代码的Activity上并没有设置任务affinity,因此我也不确定为何会有影响。)
站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。