Commit e6f711a1 authored by yangwu.jia's avatar yangwu.jia

Add BoostLifecycleListener

parent 45482261
......@@ -64,7 +64,7 @@ public class NewFlutterBoost {
if (mCurrentActiveActivity == null) {
Debuger.log("Application entry foreground");
if (NewFlutterBoost.instance().engineProvider() != null) {
if (createEngine() != null) {
HashMap<String, String> map = new HashMap<>();
map.put("type", "foreground");
channel().sendEvent("lifecycle", map);
......@@ -88,7 +88,7 @@ public class NewFlutterBoost {
if (mCurrentActiveActivity == activity) {
Debuger.log("Application entry background");
if (engineProvider() != null) {
if (createEngine() != null) {
HashMap<String, String> map = new HashMap<>();
map.put("type", "background");
channel().sendEvent("lifecycle", map);
......@@ -107,7 +107,7 @@ public class NewFlutterBoost {
if (mCurrentActiveActivity == activity) {
Debuger.log("Application entry background");
if (engineProvider() != null) {
if (createEngine() != null) {
HashMap<String, String> map = new HashMap<>();
map.put("type", "background");
channel().sendEvent("lifecycle", map);
......@@ -131,8 +131,10 @@ public class NewFlutterBoost {
if(mEngine!=null) return;
FlutterEngine flutterEngine = engineProvider();
FlutterEngine flutterEngine = createEngine();
if(mPlatform.lifecycleListener!=null){
mPlatform.lifecycleListener.onEngineCreated();
}
if (flutterEngine.getDartExecutor().isExecutingDart()) {
// No warning is logged because this situation will happen on every config
// change if the developer does not choose to retain the Fragment instance.
......@@ -154,12 +156,16 @@ public class NewFlutterBoost {
);
flutterEngine.getDartExecutor().executeDartEntrypoint(entrypoint);
mRegistry = new BoostPluginRegistry(engineProvider(),mPlatform.getApplication());
mRegistry = new BoostPluginRegistry(createEngine(),mPlatform.getApplication());
((BoostPluginRegistry) mRegistry).currentActivity(mCurrentActiveActivity);
mPlatform.registerPlugins(mRegistry);
if(mPlatform.lifecycleListener!=null){
mPlatform.lifecycleListener.onPluginsRegistered();
}
}
......@@ -172,13 +178,12 @@ public class NewFlutterBoost {
public static int ANY_ACTIVITY_CREATED = 1; //当有任何Activity创建时,启动引擎
public static int FLUTTER_ACTIVITY_CREATED = 2; //当有任何Activity创建时,启动引擎
public static int FLUTTER_ACTIVITY_CREATED = 2; //当有flutterActivity创建时,启动引擎
public static int APP_EXit = 0; //所有flutter Activity destory 时,销毁engine
public static int All_FLUTTER_ACTIVITY_DESTROY = 1; //所有flutter Activity destory 时,销毁engine
private String dartEntrypoint = DEFAULT_DART_ENTRYPOINT;
private String initialRoute = DEFAULT_INITIAL_ROUTE;
private int whenEngineStart = ANY_ACTIVITY_CREATED;
......@@ -193,6 +198,8 @@ public class NewFlutterBoost {
private INativeRouter router = null;
private BoostLifecycleListener lifecycleListener;
public ConfigBuilder(Application app, INativeRouter router) {
this.router = router;
this.mApp = app;
......@@ -218,14 +225,19 @@ public class NewFlutterBoost {
return this;
}
public ConfigBuilder whenEngineStart(@NonNull int whenEngineStart) {
public ConfigBuilder whenEngineStart( int whenEngineStart) {
this.whenEngineStart = whenEngineStart;
return this;
}
public ConfigBuilder whenEngineDestory(@NonNull int whenEngineDestory) {
public ConfigBuilder whenEngineDestory( int whenEngineDestory) {
this.whenEngineDestory = whenEngineDestory;
return this;
}
public ConfigBuilder lifecycleListener( BoostLifecycleListener lifecycleListener) {
this.lifecycleListener = lifecycleListener;
return this;
}
public Platform build() {
Platform platform = new Platform() {
......@@ -263,6 +275,8 @@ public class NewFlutterBoost {
}
};
platform.lifecycleListener=this.lifecycleListener;
return platform;
}
......@@ -293,8 +307,7 @@ public class NewFlutterBoost {
return mRegistry;
}
public FlutterEngine engineProvider() {
private FlutterEngine createEngine(){
if (mEngine == null) {
FlutterMain.startInitialization(mPlatform.getApplication());
......@@ -304,23 +317,33 @@ public class NewFlutterBoost {
mPlatform.getApplication().getApplicationContext(), flutterShellArgs.toArray());
mEngine = new FlutterEngine(mPlatform.getApplication().getApplicationContext());
}
return mEngine;
}
public FlutterEngine engineProvider() {
return mEngine;
}
public void boostDestroy(){
if(mEngine!=null){
mEngine.destroy();
}
if(mPlatform.lifecycleListener!=null){
mPlatform.lifecycleListener.onEngineDestroy();
}
mEngine=null;
mRegistry=null;
mCurrentActiveActivity=null;
}
public interface BoostLifecycleListener {
void onEngineCreated();
void onPluginsRegistered();
void onEngineDestroy();
}
}
......@@ -25,6 +25,7 @@ public abstract class Platform {
public abstract String initialRoute();
public NewFlutterBoost.BoostLifecycleListener lifecycleListener;
public void closeContainer(IContainerRecord record, Map<String, Object> result, Map<String, Object> exts) {
if (record == null) return;
......
......@@ -3,6 +3,7 @@ package com.taobao.idlefish.flutterboostexample;
import android.app.Application;
import android.content.Context;
import android.util.Log;
import com.idlefish.flutterboost.*;
import com.idlefish.flutterboost.interfaces.IContainerRecord;
......@@ -10,12 +11,14 @@ import java.util.Map;
import com.idlefish.flutterboost.interfaces.INativeRouter;
import io.flutter.app.FlutterApplication;
import io.flutter.plugin.common.MethodChannel;
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
INativeRouter router =new INativeRouter() {
@Override
public void openContainer(Context context, String url, Map<String, Object> urlParams, int requestCode, Map<String, Object> exts) {
......@@ -25,12 +28,34 @@ public class MyApplication extends Application {
};
NewFlutterBoost.BoostLifecycleListener lifecycleListener= new NewFlutterBoost.BoostLifecycleListener() {
@Override
public void onEngineCreated() {
}
@Override
public void onPluginsRegistered() {
MethodChannel mMethodChannel = new MethodChannel( NewFlutterBoost.instance().engineProvider().getDartExecutor(), "boosttest");
Log.e("MyApplication","MethodChannel create");
}
@Override
public void onEngineDestroy() {
}
};
Platform platform= new NewFlutterBoost
.ConfigBuilder(this,router)
.isDebug(true)
.whenEngineStart(NewFlutterBoost.ConfigBuilder.ANY_ACTIVITY_CREATED)
.lifecycleListener(lifecycleListener)
.build();
NewFlutterBoost.instance().init(platform);
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment