You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android 14(API34)下AppAuth组件通信及跳转异常问题求助

解决方案:Android 14 下独立taskAffinity页面发起Google认证无法返回原页面问题

问题核心

Android 14(API 34)对任务栈管理逻辑做了调整,当带有独立taskAffinity的Activity发起外部认证请求时,Auth库的状态存储无法关联到目标任务栈,导致回调时找不到原页面,出现No stored state - unable to handle response警告并创建新任务实例。

具体修复步骤

  • 调整Activity B的Manifest配置
    在AndroidManifest.xml中给Activity B设置启动模式为singleTask,保留独立taskAffinity:

    <activity
        android:name=".ActivityB"
        android:taskAffinity="com.your.app.task.b"
        android:launchMode="singleTask">
    </activity>
    
  • 显式指定认证回调的返回目标
    发起AuthRequest时,通过PendingIntent明确指定返回Activity B,确保回调逻辑能定位到正确的任务栈:

    // 构建返回Activity B的Intent
    val returnIntent = Intent(this@ActivityB, ActivityB::class.java).apply {
        flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
    }
    // 创建PendingIntent(API31+必须使用FLAG_IMMUTABLE)
    val pendingIntent = PendingIntent.getActivity(
        this@ActivityB,
        0,
        returnIntent,
        PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
    )
    // 构建AuthRequest时绑定返回PendingIntent
    val authRequest = AuthorizationRequest.Builder(
        clientId,
        AuthorizationRequest.ResponseType.CODE,
        redirectUri
    )
        .setReturnPendingIntent(pendingIntent)
        .build()
    // 发起认证请求
    AuthorizationService(this@ActivityB).performAuthorizationRequest(authRequest)
    
  • 修正Auth库相关Activity的Manifest配置
    将AuthorizationManagementActivity和RedirectUriReceiverActivity的taskAffinity设为默认(空字符串),并启用任务重归属,避免创建孤立任务栈:

    <activity
        android:name="net.openid.appauth.AuthorizationManagementActivity"
        android:taskAffinity=""
        android:allowTaskReparenting="true"
        android:launchMode="singleTask"/>
    <activity
        android:name="net.openid.appauth.RedirectUriReceiverActivity"
        android:taskAffinity=""
        android:allowTaskReparenting="true"
        android:launchMode="singleTask"/>
    

验证注意事项

  • 测试前清除应用缓存,确保任务栈状态干净
  • 确认Google OAuth配置中的重定向URI与Manifest中RedirectUriReceiverActivity的配置完全一致

内容的提问来源于stack exchange,提问作者Anthony Koueik

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 07:52:39