简体   繁体   中英

How to call flutter module to android activity in flutter

I have successfully integrated flutter module in my native android application. but I want to call flutter module to android's module second activity.

for ex. - Android module have a two activity, first is MainActivty and other is Second Activity. When user click on MainActivity's screen button flutter module open again user click on flutter's module floating button then SecondActivity will be open.

How can I achieve this, please help me.

This is flutter code for go to android:

  InkWell(
            onTap: () {
              _showNativeView();
            },
            child:Container(),
          )

This is android native class code:

public class MainActivity extends FlutterActivity {

public static final String FLUTTER_TO_ANDROID = "flutterToandroid";


@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    GeneratedPluginRegistrant.registerWith(new FlutterEngine(this));

    // this is for open customer application with staging url
    new MethodChannel(getFlutterEngine().getDartExecutor().getBinaryMessenger(), FLUTTER_TO_ANDROID).setMethodCallHandler(new MethodChannel.MethodCallHandler() {
        @Override
        public void onMethodCall(@NonNull MethodCall methodCall, @NonNull MethodChannel.Result result) {

            if (methodCall.method.equals("showNativeView")) {
                String messagetoflutter = showNativeView();
                result.success(messagetoflutter);
            } else {
                result.notImplemented();
            }

        }
    });
}

String showNativeView() {

    try {



       Intent i = newIntent(MainActivity.this,SecondActivity.class);
        startActivity(i);

        Toast.makeText(this, "Done, you are Correct",Toast.LENGTH_SHORT).show();

    } catch (Exception e) {
        Toast.makeText(this, "Application not found on this device", Toast.LENGTH_SHORT).show();
    }
    return "okay";
}

}

When showNativeView Method call then it go to SecondActivity.

Firstly you should create a FlutterMethodChannel then Invoke method on platform Channel

String response = "";

try {
    final String result = await platform.invokeMethod('helloFromNativeCode');
   
 response = result;

  } on PlatformException catch (e) {

    response = "Failed to Invoke: '${e.message}'.";
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM