Search code examples
flutterkotlinbridge

Can you return a result back to Flutter from Native Kotlin Bridge


at the moment I have a Flutter App that when I press a button it opens a Native Kotlin activity(mainActivity), once this executes I automatically open a SecondActivity that extends AppCompatActivity, the issue becomes is that I want to return a bool from the SecondActivity back to Flutter.

At the moment when the process finishes i use "finish()" to close my Native code and return to flutter successfully but I'd like to pass a result to Flutter.

I know how to do it from the mainActivity as it uses the FlutterActivity directly and can pass "result.success()", but from the different activity I can't find a solution.

I was wondering if somebody can tell me how to pass the bool from the secondActivity to Flutter.

Thank you very much everyone in advance!


Solution

  • It works exactly the same. Store Result in class property and call one of its methods from onActivityResult when the second activity finishes. Something like this:

    class YourPlugin ... {
    ...
      private var pendingResult: Result? = null
    ...
    
      override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {
    ...
        pendingResult = result
        activity?.startActivityForResult(...)
    ...
      }
    
      override fun onActivityResult(requestCode: Int, resultCode: Int, intent: Intent?): Boolean {
    ...
        pendingResult?.success(<result>)
    ...
        return false
      }