Open Android Activity and iOS ViewController from Flutter

Published

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?

Source: Ios Questions

Published
Categorised as android, flutter, ios Tagged , ,

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


Dr. Amelie Schmidt

Leave a Reply

Your email address will not be published. Required fields are marked *

Still Have Questions?


Our dedicated development team is here for you!

We can help you find answers to your question for as low as 5$.

Contact Us
faq