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

Flutter集成DocuSign SDK离线签名启动Activity报错求助

解决Flutter插件中DocuSign离线签名启动Activity的Context错误

你遇到的错误核心原因是使用ApplicationContext启动Activity:Android规定,从非Activity类型的上下文启动Activity时,必须添加FLAG_ACTIVITY_NEW_TASK标志,但这种做法会破坏Activity栈的正常管理;更规范且用户体验更好的方式是直接使用Activity类型的Context。

推荐解决方案:获取Flutter关联的Activity上下文

Flutter插件可以通过实现ActivityAware接口来获取绑定的Activity实例,用它作为启动签名页面的上下文,步骤如下:

修改插件代码

class DocusignFlutterPlugin: FlutterPlugin, MethodCallHandler, ActivityAware {
  private lateinit var channel: MethodChannel
  private var activity: Activity? = null // 保存Activity实例
  private var context: Context? = null

  override fun onAttachedToEngine(flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
    channel = MethodChannel(flutterPluginBinding.binaryMessenger, "docusign_flutter")
    channel.setMethodCallHandler(this)
    context = flutterPluginBinding.applicationContext
  }

  // Activity绑定到插件时触发,保存Activity实例
  override fun onAttachedToActivity(binding: ActivityPluginBinding) {
    activity = binding.activity
  }

  override fun onDetachedFromActivityForConfigChanges() {
    activity = null
  }

  override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) {
    activity = binding.activity
  }

  override fun onDetachedFromActivity() {
    activity = null
  }

  override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) {
    channel.setMethodCallHandler(null)
  }

  private fun signEnvelopeOfflineById(call: MethodCall, result: Result) {
    val envelopeId = call.argument<String>("envelopeId")!!
    // 先检查Activity是否可用
    val currentActivity = activity ?: run {
      result.error("NO_ACTIVITY", "无法获取Activity上下文", null)
      return
    }

    DocuSign.getInstance().getSigningDelegate().signOffline(currentActivity, envelopeId, object : DSOfflineSigningListener{
      override fun onCancel(envelopeId: String) {
        result.success(mapOf("status" to "cancelled", "envelopeId" to envelopeId))
      }

      override fun onError(exception: DSSigningException) {
        result.error("SIGN_ERROR", exception.message, exception)
      }

      override fun onSuccess(envelopeId: String) {
        result.success(mapOf("status" to "success", "envelopeId" to envelopeId))
      }
    })
  }
}

为什么这个方案更好

Activity上下文能正确维护App的Activity栈,用户完成离线签名后可以正常返回Flutter页面;而用ApplicationContext加FLAG_ACTIVITY_NEW_TASK的方式,会把签名页面放到新的任务栈,用户返回时可能直接回到桌面,破坏预期的交互流程。

临时 fallback 方案:添加NEW_TASK标志(不推荐)

如果暂时无法获取Activity上下文,可尝试给启动签名页面的Intent添加FLAG_ACTIVITY_NEW_TASK标志,但仅作为临时应急方案:

// 若能修改DocuSign SDK的启动逻辑,可给Intent加标志
val intent = Intent(context, OfflineSigningActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
context.startActivity(intent)

注意:由于你使用的是DocuSign封装好的SDK方法,可能无法直接修改Intent,因此优先选择第一种方案。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 14:55:00