在跳转之前需要先在AndroidManifest.xml注册FlutterActivity。
在需要跳转的时候,使用FlutterActivity.withNewEngine()进行跳转:
startActivity(FlutterActivity.withNewEngine()
.initialRoute(“params”)
.build(xxxxActivity.this));
其中initialRoute是Android跳转到flutter需要的参数,非必需。 在flutter接收参数如下
…
class _MyHomePageState extends State {
String route = window.defaultRouteName;
…
}
window.defaultRouteName就是获取Android传递过来的参数,当Android端需要跳转多个flutter页面,通常这个用于路由分发,若需要的信息比较多的时候可以传递json字符串。注:window.defaultRouteName的导包为’dart:ui’,而不是’dart:html’。
- FlutterActivity间接跳转
所谓的间接跳转其实就是通过继承FlutterActivity来实现的
public class Hybrid extends FlutterActivity {
public final static String PARAMS = “params”;
private String params;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
params = getIntent().getStringExtra(PARAMS);
}
@NonNu
《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享
ll
@Override
public String getInitialRoute() {
return params == null ? super.getInitialRoute() : params;
}
public static void toFlutter(Context context, String params) {
Intent intent = new Intent(context, Hybrid.class);
intent.putExtra(PARAMS, params);
context.startActivity(intent);
}
}
在AndroidManifest.xm注册Hybrid后就可以通过
Hybrid.toFlutter(xxxxxActivity.this,“params”);
进行跳转。
- 通过FlutterEngine和BasicMessageChannel进行跳转
为什么会有这种方式,是因为使用前两种方式跳转时会有短暂的空白,使用起来感觉非常不流畅,严重影响用户体验。这种跳转方式和上面的两种不同,边跳边发,适用于多种场景。这里也不一定非要使用BasicMessageChannel,也可使用MethodChannel或EventChannel,只是混合开发通常涉及到两端频繁通信,个人更加倾向使用BasicMessageChannel,不分主客,使用和通信更方便。
- FlutterEngine和BasicMessageChannel的初始化和使用
public class HyBridRouteAndMessageHandleCenter{
private static FlutterEngine flutterEngine;
private static BasicMessageChannel basicMessageChannel;
private static String channelName = “com.hybrid.basic.message.channel”;
public static final String ENGINE_ID = “default_engine_id”;
private static Context mContext;
private static BasicMessageChannel.Reply mReplay;
public static void init(Context context) {
mContext = context;
if (flutterEngine == null) {
flutterEngine = new FlutterEngine(context);
flutterEngine.getDartExecutor().executeDartEntrypoint(DartExecutor.DartEntrypoint.createDefault());
FlutterEngineCache.getInstance().put(ENGINE_ID, flutterEngine);
}
if (basicMessageChannel == null) {
if (flutterEngine != null) {
basicMessageChannel = new BasicMessageChannel(flutterEngine.getDartExecutor(), channelName, StandardMessageCodec.INSTANCE);
basicMessageChannel.setMessageHandler(new BasicMessageChannel.MessageHandler() {
@Override
public void onMessage(@Nullable @org.jetbrains.annotations.Nullable Object message, @NonNull @NotNull BasicMessageChannel.Reply reply) {
mReplay = reply;
// 接收消息并处理
handleMessage(message, reply);
}
});
}
}
}
// 处理消息
private static void handleMessage(Object message, BasicMessageChannel.Reply reply) {
…
}
public static void toFlutter(Context context, Object params) {
sendMessage(params);
context.startActivity(FlutterActivity.withCachedEngine(ENGINE_ID).build(context));
}
public static void sendMessage(Object object) {
if (basicMessageChannel != null) {
basicMessageChannel.send(object, new BasicMessageChannel.Reply() {
@Override
public void reply(@Nullable @org.jetbrains.annotations.Nullable Object reply) {
// 发送回调
…
}
});
}
}
public static void destroyEngine() {
if (flutterEngine != null) {
flutterEngine.destroy();
}
}
}