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

Add BoostLifecycleListener

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