IPlatform
移除一下代码
Activity getMainActivity();
boolean startActivity(Context context,String url,int requestCode);
Map getSettings();
新增加
void registerPlugins(PluginRegistry registry) 方法
void openContainer(Context context,String url,Map urlParams,int requestCode,Map exts);
void closeContainer(IContainerRecord record, Map result, Map exts);
IFlutterEngineProvider engineProvider();
int whenEngineStart();
这个Api变化主要是在Application初始化的时候的修改
以上是Flutter_boost github上面的说明,具体可以查看
接入的方式主要是在V0.0.420版本上面做的修改,V0.0.420的接入方式可以参考我之前的文章
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
FlutterBoostPlugin.init(new IPlatform() {
@Override
public Application getApplication() {
return MyApplication.this;
}
/**
* get the main activity, this activity should always at the bottom of task stack.
*/
@Override
public Activity getMainActivity() {
return MainActivity.sRef.get();
}
@Override
public boolean isDebug() {
return false;
}
/**
* start a new activity from flutter page, you may need a activity router.
*/
@Override
public boolean startActivity(Context context, String url, int requestCode) {
return PageRouter.openPageByUrl(context,url,requestCode);
}
@Override
public Map getSettings() {
return null;
}
});
}
}
可以继承FlutterApplication,直接使用,不继承FlutterApplication 的话需要在使用Flutter的Activity中onCreate加入 FlutterMain.startInitialization(this);
如下所示
@CallSuper
public void onCreate() {
FlutterMain.startInitialization(this);
super.onCreate();
}
其实对应的FlutterApplication是很简单的,主要还是在onCreate中
public class FlutterApplication extends Application {
private Activity mCurrentActivity = null;
public FlutterApplication() {
}
@CallSuper
public void onCreate() {
super.onCreate();
FlutterMain.startInitialization(this);
}
public Activity getCurrentActivity() {
return this.mCurrentActivity;
}
public void setCurrentActivity(Activity mCurrentActivity) {
this.mCurrentActivity = mCurrentActivity;
}
}
public class MyApplication extends FlutterApplication {
@Override
public void onCreate() {
super.onCreate();
FlutterBoost.init(new Platform() {
@Override
public Application getApplication() {
return MyApplication.this;
}
@Override
public boolean isDebug() {
return true;
}
@Override
public void openContainer(Context context, String url, Map urlParams, int requestCode, Map exts) {
PageRouter.openPageByUrl(context,url,urlParams,requestCode);
}
@Override
public IFlutterEngineProvider engineProvider() {
return new BoostEngineProvider(){
@Override
public BoostFlutterEngine createEngine(Context context) {
return new BoostFlutterEngine(context, new DartExecutor.DartEntrypoint(
context.getResources().getAssets(),
FlutterMain.findAppBundlePath(context),
"main"),"/");
}
};
}
@Override
public int whenEngineStart() {
return IMMEDIATELY;
}
});
}
}
移除和新增代码,前面讲了,这里主要讲一下whenEngineStart()函数,其返回值有两个
int IMMEDIATELY = 0; //立即启动引擎
int ANY_ACTIVITY_CREATED = 1; //当有任何Activity创建时,启动引擎
官方demo给的默认值是ANY_ACTIVITY_CREATED 创建Activity 都会启动,主要是控制引擎的初始化时机
engineProvider()返回一个Flutter引擎提供者,BoostEngineProvider,主要看其内部的createEngine()方法
@Override
public BoostFlutterEngine createEngine(Context context) {
return new BoostFlutterEngine(context, new DartExecutor.DartEntrypoint(
context.getResources().getAssets(),
FlutterMain.findAppBundlePath(context),
"main"),"/");
}
这里大致讲一下其中的main字符,这个主要是在BoostFlutterEngine内部创建引擎的时候,设置对应的Flutter 执行的main函数也就是Flutter代码的执行的入口,斜杠主要是初始化路由用的
修改版本信息v0.1.52,最新版
移除
FlutterBoost.handleOnStartPage();
打开页面不带参数
FlutterBoost.singleton.openPage("second", {}, animated: true);
FlutterBoost.singleton.open("second")
打开页面带参数
FlutterBoost.singleton.openPage("sample://nativePage", {
"query": {"aaa": "bbb"}
}),
FlutterBoost.singleton.open("sample://nativePage", urlParams:{
"query": {"aaa": "bbb"}
}),
关闭页面
FlutterBoost.singleton.closePageForContext(context);
BoostContainerSettings settings = BoostContainer.of(context).settings;
FlutterBoost.singleton.close(settings.uniqueId,result: {"result":"data from second"});
写好flutter插件相关的之后,再iOS端接入就比较简单了
再命令行执行
pod install
就会升级完flutter_boost的插件,主要再API层面的改动和Android差不多,就是open/close两个方法,具体如下
升级前
- (void)openPage:(NSString *)name params:(NSString *)json animated:(BOOL)animated completion:(void (^)(BOOL))completion
- (void)closePage:(NSString *)uid
animated:(BOOL)animated
params:(NSDictionary *)params
completion:(void (^)(BOOL finished))completion;
升级后
#pragma mark - Boost 1.5
- (void)open:(NSString *)name
urlParams:(NSDictionary *)params
exts:(NSDictionary *)exts
completion:(void (^)(BOOL))completion
- (void)close:(NSString *)uid
result:(NSDictionary *)result
exts:(NSDictionary *)exts
completion:(void (^)(BOOL finished))completion;
//继承BoostFlutterActivity修改
BoostFlutterActivity#createRenderingProgressCover()方法可以避免
2.同样的低端机器,再native界面回退到Flutter页面会存在白屏闪一下的现象,应该是在恢复Flutter相关的绘制时候导致的,期待后续的优化