Android OnActivityResult gave me a NULL value with getStringExtra()

160 Views Asked by At

I'm trying to get a response inside the onActivityResult from an uri intent. However, it is always -1 as resultCode and null as Intent data.

  private fun startMyFunction(call: MethodCall) {
    val app: String? = call.argument("app")
    val url: String? = call.argument("url")

    try {
      val uri = Uri.parse(url)
      
      val intent = Intent(Intent.ACTION_VIEW, uri)
      intent.setPackage(app)

      if (activity?.let { intent.resolveActivity(it.packageManager) } == null) {
        this.success("activity_unavailable")
        return
      }

      activity?.startActivityForResult(intent, requestCodeNumber)
    } catch (ex: Exception) {
      Log.e("my_app", ex.toString())
      this.success("failed_to_open_app")
    }
  }
  private fun success(o: String) {
    if (!hasResponded) {
      hasResponded = true
      result?.success(o)
    }
  }
  override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?): Boolean {
    if (requestCodeNumber == requestCode && result != null) {
      Log.d("onActivityResult - response - ", data.toString())
      if (data != null) {
        try {
          val response = data.getStringExtra("response")!!
          this.success(response) 
        } catch (ex: Exception) {
          this.success("invalid_response - Exception")
        }
      } else {
        this.success("user_cancelled - Null data")
      }
    }
    return true
  }

I have read many of the similar situations here on StackOverflow, as well as on Google, but none of them have helped me in my case

For ref - https://github.com/GJJ2019/upi_pay/blob/master/android/src/main/kotlin/com/drenther/upi_pay/UpiPayPlugin.kt

0

There are 0 best solutions below