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

解决registerPlugins 时候,必须attach activity

parent 08956ada
...@@ -12,10 +12,12 @@ import io.flutter.embedding.android.FlutterView; ...@@ -12,10 +12,12 @@ import io.flutter.embedding.android.FlutterView;
import io.flutter.embedding.engine.FlutterEngine; import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.embedding.engine.FlutterShellArgs; import io.flutter.embedding.engine.FlutterShellArgs;
import io.flutter.embedding.engine.dart.DartExecutor; import io.flutter.embedding.engine.dart.DartExecutor;
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
import io.flutter.embedding.engine.plugins.shim.ShimPluginRegistry; import io.flutter.embedding.engine.plugins.shim.ShimPluginRegistry;
import io.flutter.plugin.common.PluginRegistry; import io.flutter.plugin.common.PluginRegistry;
import io.flutter.view.FlutterMain; import io.flutter.view.FlutterMain;
import java.lang.reflect.Method;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
...@@ -27,8 +29,9 @@ public class NewFlutterBoost { ...@@ -27,8 +29,9 @@ public class NewFlutterBoost {
private FlutterEngine mEngine; private FlutterEngine mEngine;
private Activity mCurrentActiveActivity; private Activity mCurrentActiveActivity;
private PluginRegistry mRegistry; private PluginRegistry mRegistry;
static NewFlutterBoost sInstance = null; private boolean hasRegistry=false;
static NewFlutterBoost sInstance = null;
public static NewFlutterBoost instance() { public static NewFlutterBoost instance() {
if (sInstance == null) { if (sInstance == null) {
...@@ -154,19 +157,11 @@ public class NewFlutterBoost { ...@@ -154,19 +157,11 @@ public class NewFlutterBoost {
FlutterMain.findAppBundlePath(), FlutterMain.findAppBundlePath(),
"main" "main"
); );
flutterEngine.getDartExecutor().executeDartEntrypoint(entrypoint); flutterEngine.getDartExecutor().executeDartEntrypoint(entrypoint);
mRegistry = new BoostPluginRegistry(createEngine()); mRegistry = new BoostPluginRegistry(createEngine());
// ((BoostPluginRegistry) mRegistry).currentActivity(mCurrentActiveActivity);
mPlatform.registerPlugins(mRegistry);
if(mPlatform.lifecycleListener!=null){
mPlatform.lifecycleListener.onPluginsRegistered();
}
} }
...@@ -322,6 +317,25 @@ public class NewFlutterBoost { ...@@ -322,6 +317,25 @@ public class NewFlutterBoost {
} }
public void registerPlugins() {
if(!hasRegistry&&mRegistry!=null){
try {
Class clz = Class.forName("io.flutter.plugins.GeneratedPluginRegistrant");
Method method = clz.getDeclaredMethod("registerWith", PluginRegistry.class);
method.invoke(null, mRegistry);
} catch (Throwable t) {
throw new RuntimeException(t);
}
if(mPlatform.lifecycleListener!=null){
mPlatform.lifecycleListener.onPluginsRegistered();
}
hasRegistry=true;
}
}
public FlutterEngine engineProvider() { public FlutterEngine engineProvider() {
return mEngine; return mEngine;
} }
......
...@@ -34,15 +34,7 @@ public abstract class Platform { ...@@ -34,15 +34,7 @@ public abstract class Platform {
} }
public void registerPlugins(PluginRegistry registry) {
try {
Class clz = Class.forName("io.flutter.plugins.GeneratedPluginRegistrant");
Method method = clz.getDeclaredMethod("registerWith", PluginRegistry.class);
method.invoke(null, registry);
} catch (Throwable t) {
throw new RuntimeException(t);
}
}
} }
...@@ -130,6 +130,12 @@ public class FlutterActivityAndFragmentDelegate implements IFlutterViewContaine ...@@ -130,6 +130,12 @@ public class FlutterActivityAndFragmentDelegate implements IFlutterViewContaine
@NonNull @NonNull
View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
Log.v(TAG, "Creating FlutterView."); Log.v(TAG, "Creating FlutterView.");
flutterEngine.getActivityControlSurface().attachToActivity(
host.getActivity(),
host.getLifecycle()
);
NewFlutterBoost.instance().registerPlugins();
mSyncer = NewFlutterBoost.instance().containerManager().generateSyncer(this); mSyncer = NewFlutterBoost.instance().containerManager().generateSyncer(this);
...@@ -173,12 +179,17 @@ public class FlutterActivityAndFragmentDelegate implements IFlutterViewContaine ...@@ -173,12 +179,17 @@ public class FlutterActivityAndFragmentDelegate implements IFlutterViewContaine
ensureAlive(); ensureAlive();
flutterEngine.getLifecycleChannel().appIsResumed(); flutterEngine.getLifecycleChannel().appIsResumed();
BoostPluginRegistry registry= (BoostPluginRegistry)NewFlutterBoost.instance().getPluginRegistry();
ActivityPluginBinding binding=registry.getRegistrarAggregate().getActivityPluginBinding();
if(binding!=null&&(binding.getActivity()!=this.host.getActivity())){
flutterEngine.getActivityControlSurface().attachToActivity( flutterEngine.getActivityControlSurface().attachToActivity(
host.getActivity(), host.getActivity(),
host.getLifecycle() host.getLifecycle()
); );
} }
}
void onPostResume() { void onPostResume() {
Log.v(TAG, "onPostResume()"); Log.v(TAG, "onPostResume()");
......
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