Support a model where the application creates a FlutterNativeView that is never destroyed (flutter/engine#5256)

This allows applications to start a Flutter engine instance during app startup
and keep it running throughout the app process' lifetime.

FlutterActivity subclasses can override createFlutterNativeView to provide a
preinitialized FlutterNativeView instance and override retainFlutterNativeView
to signal that the FlutterNativeView should be kept alive when the activity
is destroyed.
This commit is contained in:
Jason Simmons
2018-05-15 12:05:54 -07:00
committed by GitHub
parent 2f8d2f374e
commit 4f15413841
4 changed files with 41 additions and 4 deletions

View File

@@ -60,6 +60,11 @@ public class FlutterActivity extends Activity implements FlutterView.Provider, P
return null;
}
@Override
public boolean retainFlutterNativeView() {
return false;
}
@Override
public final boolean hasPlugin(String key) {
return pluginRegistry.hasPlugin(key);

View File

@@ -85,6 +85,13 @@ public final class FlutterActivityDelegate
public interface ViewFactory {
FlutterView createFlutterView(Context context);
FlutterNativeView createFlutterNativeView();
/**
* Hook for subclasses to indicate that the {@code FlutterNativeView}
* returned by {@link #createFlutterNativeView()} should not be destroyed
* when this activity is destroyed.
*/
boolean retainFlutterNativeView();
}
private final Activity activity;
@@ -172,9 +179,11 @@ public final class FlutterActivityDelegate
if (loadIntent(activity.getIntent(), reuseIsolate)) {
return;
}
String appBundlePath = FlutterMain.findAppBundlePath(activity.getApplicationContext());
if (appBundlePath != null) {
if (!flutterView.getFlutterNativeView().isApplicationRunning()) {
String appBundlePath = FlutterMain.findAppBundlePath(activity.getApplicationContext());
if (appBundlePath != null) {
flutterView.runFromBundle(appBundlePath, null, "main", reuseIsolate);
}
}
}
@@ -250,7 +259,7 @@ public final class FlutterActivityDelegate
if (flutterView != null) {
final boolean detach =
flutterView.getPluginRegistry().onViewDestroy(flutterView.getFlutterNativeView());
if (detach) {
if (detach || viewFactory.retainFlutterNativeView()) {
// Detach, but do not destroy the FlutterView if a plugin
// expressed interest in its FlutterNativeView.
flutterView.detach();
@@ -343,7 +352,9 @@ public final class FlutterActivityDelegate
if (route != null) {
flutterView.setInitialRoute(route);
}
flutterView.runFromBundle(appBundlePath, intent.getStringExtra("snapshot"), "main", reuseIsolate);
if (!flutterView.getFlutterNativeView().isApplicationRunning()) {
flutterView.runFromBundle(appBundlePath, intent.getStringExtra("snapshot"), "main", reuseIsolate);
}
return true;
}

View File

@@ -64,6 +64,11 @@ public class FlutterFragmentActivity
return null;
}
@Override
public boolean retainFlutterNativeView() {
return false;
}
@Override
public final boolean hasPlugin(String key) {
return pluginRegistry.hasPlugin(key);

View File

@@ -26,6 +26,7 @@ public class FlutterNativeView implements BinaryMessenger {
private long mNativePlatformView;
private FlutterView mFlutterView;
private final Context mContext;
private boolean applicationIsRunning;
public FlutterNativeView(Context context) {
mContext = context;
@@ -45,6 +46,7 @@ public class FlutterNativeView implements BinaryMessenger {
mFlutterView = null;
nativeDestroy(mNativePlatformView);
mNativePlatformView = 0;
applicationIsRunning = false;
}
public FlutterPluginRegistry getPluginRegistry() {
@@ -71,12 +73,26 @@ public class FlutterNativeView implements BinaryMessenger {
public void runFromBundle(String bundlePath, String snapshotOverride, String entrypoint, boolean reuseRuntimeController) {
assertAttached();
if (applicationIsRunning)
throw new AssertionError("This Flutter engine instance is already running an application");
nativeRunBundleAndSnapshot(mNativePlatformView, bundlePath, snapshotOverride, entrypoint, reuseRuntimeController, mContext.getResources().getAssets());
applicationIsRunning = true;
}
public void runFromSource(final String assetsDirectory, final String main, final String packages) {
assertAttached();
if (applicationIsRunning)
throw new AssertionError("This Flutter engine instance is already running an application");
nativeRunBundleAndSource(mNativePlatformView, assetsDirectory, main, packages);
applicationIsRunning = true;
}
public boolean isApplicationRunning() {
return applicationIsRunning;
}
public void setAssetBundlePathOnUI(final String assetsDirectory) {