Flutter跳转原生Activity

Flutter跳转原生Activity_第1张图片
image.png

Flutter跳转原生Activity_第2张图片
image.png

通过MethodChannel与安卓原生进行通信

Flutter代码:

先写一个通道类,定义通道名(plugin_demo)和方法名(jumpToActivity),通道是异步的所以要用async await

class PluginManager {
  static const MethodChannel _channel =
      const MethodChannel('plugin_demo');

  static Future pushFirstActivity(Map params) async {
    String resultStr = await _channel.invokeMethod('jumpToActivity', params);
    return resultStr;
  }
}

随便写个按钮调用通道:

onPressed: () async {
    String resultString;
    try {
      resultString = await PluginManager.pushFirstActivity({'key': 'value'});
    } on PlatformException {
      resultString = '失败';
    }
    print(resultString);
}

安卓代码:

找到项目中的安卓MainActivity,点击右上角的在Android Studio中打开,等待gradle自动配置好,红色报错就没了


Flutter跳转原生Activity_第3张图片
image.png

MainActivity代码:

package com.example.flutter_jumpto_android;

import android.content.Intent;

import io.flutter.embedding.android.FlutterActivity;
import androidx.annotation.NonNull;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugins.GeneratedPluginRegistrant;

public class MainActivity extends FlutterActivity {
    private static final String CHANNEL = "plugin_demo";

    @Override
    public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
        GeneratedPluginRegistrant.registerWith(flutterEngine);
        new MethodChannel(flutterEngine.getDartExecutor(), CHANNEL).setMethodCallHandler(
                new MethodChannel.MethodCallHandler() {
                    @Override
                    public void onMethodCall(MethodCall call, MethodChannel.Result result) {
                        if (call.method.equals("jumpToActivity")) {
                            // 参数
                            String params = call.argument("key");

                            // 跳转
                            Intent intent = new Intent(MainActivity.this, FirstActivity.class);
                            intent.putExtra("activityKey", params);
                            startActivity(intent);

                            result.success(params);
                        } else {
                            result.notImplemented();
                        }
                    }
                });
    }
}

在我们监听了指定channel(plugin_demo),onMethodCall中会收到来自flutter的通道信息,根据不同方法名处理不同事件,比如我们监听到jumpToActivity方法:

if (call.method.equals("jumpToActivity")) {
      // 参数
      String params = call.argument("key");

      // 跳转
      Intent intent = new Intent(MainActivity.this, FirstActivity.class);
      intent.putExtra("activityKey", params);
      startActivity(intent);

      result.success(params);
}

call.argument("key")可以获取指定key的值,call.arguments可以获取全部参数,然后使用Intent跳转:

Intent intent = new Intent(MainActivity.this, FirstActivity.class);
intent.putExtra("activityKey", params);
startActivity(intent);

处理通道消息后需要调用result.success把通信结果传给flutter

result.success("success");

顺便一问,我这跳转的Activity都没有返回键怎么回事,雕大的解答下:

public class FirstActivity extends Activity {

你可能感兴趣的:(Flutter跳转原生Activity)