Invoking Flutter method from a new Kotlin/Java Activity on button click

Viewed 834

I'm new to flutter and Kotlin.

I created a method in Flutter to invoke a Method in MainActivty.kt. This function is to start another activity from the MainActivity. The SecondAcitivty is used to take a picture using CameraX API. I want to get some results from the second activity back to flutter.

Dart function to invoke the method.

openCamera() async {
String value;
try {
  value = await platform.invokeMethod('openCamera');
} catch (e) {}

print(value);

}

My MainActivity.kt

class MainActivity: FlutterActivity() {

private val CHANNEL = "com.example.app/camera_start"
private val flutterEngine = null;

var methodCal = null;

override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
    GeneratedPluginRegistrant.registerWith(flutterEngine)
    MethodChannel(flutterEngine.dartExecutor,CHANNEL).setMethodCallHandler{
        methodCall, result ->
        if (methodCall.method == "openCamera"){

            val intent = Intent(this, CameraActivity::class.java)
            startActivityForResult(intent,2)
            result.success("Hello from Kotlin")
        }
    }



}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) {
    super.onActivityResult(requestCode, resultCode, data)
    // check if the request code is same as what is passed  here it is 2
    if (requestCode == 2) {
        val message = data.getStringExtra("MESSAGE")
        Log.d("MESSAGE", message)
    }
}

}

How can I send a value from the CameraActivity to flutter or invoke a method in flutter from a button click in CameraActivity?

0 Answers
Related