Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open Android Activity and iOS ViewController from Flutter

I have a Flutter project that requires some certain features that needs to be implemented in native Android Activity or iOS ViewController. is there a way to navigate to android Activity and pass data to it and also retrieve data from it in Flutter?

and if it's impossible, is it possible to show an Activity or fragment from Android, and a ViewController from iOS, as a Widget in Flutter?

like image 781
SinaMN75 Avatar asked Apr 09 '26 01:04

SinaMN75


2 Answers

Not sure whether this is the best way and I only created it for Android, but this is what I did.

Simple Flutter method channel calling native:

static const platform = const MethodChannel(MY_CHANNEL);
string result await platform.invokeMethod("mycall");

From the native Android part in your mainActivity:

//Class attribute
private Result myresult;

  //Method chanel
  new MethodChannel(getFlutterView(), MY_CHANNEL).setMethodCallHandler(
            (call, result) -> {
                // Note: this method is invoked on the main thread.
                if (call.method.equals("mycall")) {
                    myresult = result; //Store the flutter result
                    Intent intent1 = new Intent(MyClass.class);//Start your special native stuff
                    startActivityForResult(intent1, RQ_CODE);
                } else {
                    result.notImplemented();
                }
            });


@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    // Check which request we're responding to
    if (requestCode == RQ_CODE) {
        myresult.success("this will be your result"); //Probably do something with the data instead of a static string.
    }
}

Basically the same can be done for iOS

like image 84
Robin Dijkhof Avatar answered Apr 11 '26 18:04

Robin Dijkhof


use intent or android_intent packages for android and for ios use https://flutter.dev/docs/get-started/flutter-for/ios-devs link

like image 34
Biruk Telelew Avatar answered Apr 11 '26 17:04

Biruk Telelew



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!