Flutter_Boost v0.1.5升级 Android版本

1.新特性

  1. 支持Flutter 1.5
  2. 从老版的复用FlutterView,到新版的创建新的实例
  3. 新版创建新的的实例Flutter,可以避免白屏,闪屏,黑屏等现象
  4. 推荐使用ContainerLifeCycle observer来监听APP的生命周期
  5. 移除了xservice通信插件

2.主要的API变化

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上面的说明,具体可以查看

3.接入指南

接入的方式主要是在V0.0.420版本上面做的修改,V0.0.420的接入方式可以参考我之前的文章

旧版本 Application类的修改

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;
  }
}

新版本 Application类的修改

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代码的执行的入口,斜杠主要是初始化路由用的

4.Flutter混合库函数升级Flutter_boost

修改版本信息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_Boost v0.1.5升级 Android版本)