diff --git a/engine/src/flutter/shell/platform/android/io/flutter/app/FlutterActivity.java b/engine/src/flutter/shell/platform/android/io/flutter/app/FlutterActivity.java
index b8e9d69e46..d40dadb409 100644
--- a/engine/src/flutter/shell/platform/android/io/flutter/app/FlutterActivity.java
+++ b/engine/src/flutter/shell/platform/android/io/flutter/app/FlutterActivity.java
@@ -12,6 +12,7 @@ import android.os.Bundle;
import io.flutter.app.FlutterActivityDelegate.ViewFactory;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugin.common.PluginRegistry.Registrar;
+import io.flutter.view.FlutterNativeView;
import io.flutter.view.FlutterView;
/**
@@ -47,6 +48,18 @@ public class FlutterActivity extends Activity implements FlutterView.Provider, P
return null;
}
+ /**
+ * Hook for subclasses to customize the creation of the
+ * {@code FlutterNativeView}.
+ *
+ *
The default implementation returns {@code null}, which will cause the
+ * activity to use a newly instantiated native view object.
+ */
+ @Override
+ public FlutterNativeView createFlutterNativeView() {
+ return null;
+ }
+
@Override
public final boolean hasPlugin(String key) {
return pluginRegistry.hasPlugin(key);
@@ -68,6 +81,12 @@ public class FlutterActivity extends Activity implements FlutterView.Provider, P
eventDelegate.onCreate(savedInstanceState);
}
+ @Override
+ protected void onResume() {
+ super.onResume();
+ eventDelegate.onResume();
+ }
+
@Override
protected void onDestroy() {
eventDelegate.onDestroy();
diff --git a/engine/src/flutter/shell/platform/android/io/flutter/app/FlutterActivityDelegate.java b/engine/src/flutter/shell/platform/android/io/flutter/app/FlutterActivityDelegate.java
index 0c3d71e646..904cacb547 100644
--- a/engine/src/flutter/shell/platform/android/io/flutter/app/FlutterActivityDelegate.java
+++ b/engine/src/flutter/shell/platform/android/io/flutter/app/FlutterActivityDelegate.java
@@ -7,6 +7,7 @@ package io.flutter.app;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.app.Activity;
+import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
@@ -34,6 +35,7 @@ import io.flutter.plugin.common.PluginRegistry.RequestPermissionResultListener;
import io.flutter.plugin.platform.PlatformPlugin;
import io.flutter.util.Preconditions;
import io.flutter.view.FlutterMain;
+import io.flutter.view.FlutterNativeView;
import io.flutter.view.FlutterView;
import io.flutter.view.TextureRegistry;
@@ -75,9 +77,16 @@ public final class FlutterActivityDelegate
* A delegate's view factory will be consulted during
* {@link #onCreate(Bundle)}. If it returns {@code null}, then the delegate
* will fall back to instantiating a new full-screen {@code FlutterView}.
+ *
+ * A delegate's native view factory will be consulted during
+ * {@link #onCreate(Bundle)}. If it returns {@code null}, then the delegate
+ * will fall back to instantiating a new {@code FlutterNativeView}. This is
+ * useful for applications to override to reuse the FlutterNativeView held
+ * e.g. by a pre-existing background service.
*/
public interface ViewFactory {
FlutterView createFlutterView(Context context);
+ FlutterNativeView createFlutterNativeView();
}
private final Activity activity;
@@ -87,6 +96,7 @@ public final class FlutterActivityDelegate
private final List activityResultListeners = new ArrayList<>(0);
private final List newIntentListeners = new ArrayList<>(0);
private final List userLeaveHintListeners = new ArrayList<>(0);
+ private final List viewDestroyListeners = new ArrayList<>(0);
private FlutterView flutterView;
private View launchView;
@@ -156,7 +166,8 @@ public final class FlutterActivityDelegate
flutterView = viewFactory.createFlutterView(activity);
if (flutterView == null) {
- flutterView = new FlutterView(activity);
+ FlutterNativeView nativeView = viewFactory.createFlutterNativeView();
+ flutterView = new FlutterView(activity, null, nativeView);
flutterView.setLayoutParams(matchParent);
activity.setContentView(flutterView);
launchView = createLaunchView();
@@ -165,12 +176,18 @@ public final class FlutterActivityDelegate
}
}
- if (loadIntent(activity.getIntent())) {
+ // When an activity is created for the first time, we direct the
+ // FlutterView to re-use a pre-existing Isolate rather than create a new
+ // one. This is so that an Isolate coming in from the ViewFactory is
+ // used.
+ final boolean reuseIsolate = true;
+
+ if (loadIntent(activity.getIntent(), reuseIsolate)) {
return;
}
String appBundlePath = FlutterMain.findAppBundlePath(activity.getApplicationContext());
if (appBundlePath != null) {
- flutterView.runFromBundle(appBundlePath, null);
+ flutterView.runFromBundle(appBundlePath, null, "main", reuseIsolate);
}
}
@@ -193,6 +210,14 @@ public final class FlutterActivityDelegate
@Override
public void onPause() {
+ Application app = (Application) activity.getApplicationContext();
+ if (app instanceof FlutterApplication) {
+ FlutterApplication flutterApp = (FlutterApplication) app;
+ if (this.equals(flutterApp.getCurrentActivity())) {
+ Log.i(TAG, "onPause setting current activity to null");
+ flutterApp.setCurrentActivity(null);
+ }
+ }
if (flutterView != null) {
flutterView.onPause();
}
@@ -200,6 +225,14 @@ public final class FlutterActivityDelegate
@Override
public void onResume() {
+ Application app = (Application) activity.getApplicationContext();
+ if (app instanceof FlutterApplication) {
+ FlutterApplication flutterApp = (FlutterApplication) app;
+ Log.i(TAG, "onResume setting current activity to this");
+ flutterApp.setCurrentActivity(activity);
+ } else {
+ Log.i(TAG, "onResume app wasn't a FlutterApplication!!");
+ }
}
@Override
@@ -211,8 +244,26 @@ public final class FlutterActivityDelegate
@Override
public void onDestroy() {
+ Application app = (Application) activity.getApplicationContext();
+ if (app instanceof FlutterApplication) {
+ FlutterApplication flutterApp = (FlutterApplication) app;
+ if (this.equals(flutterApp.getCurrentActivity())) {
+ Log.i(TAG, "onDestroy setting current activity to null");
+ flutterApp.setCurrentActivity(null);
+ }
+ }
if (flutterView != null) {
- flutterView.destroy();
+ boolean destroy = true;
+ for (ViewDestroyListener listener : viewDestroyListeners) {
+ destroy = destroy && !listener.onViewDestroy(flutterView.getFlutterNativeView());
+ }
+ if (destroy) {
+ flutterView.destroy();
+ } else {
+ // Detach, but do not destroy the FlutterView if a plugin
+ // expressed interest in its FlutterNativeView.
+ flutterView.detach();
+ }
}
}
@@ -278,6 +329,11 @@ public final class FlutterActivityDelegate
}
private boolean loadIntent(Intent intent) {
+ final boolean reuseIsolate = false;
+ return loadIntent(intent, reuseIsolate);
+ }
+
+ private boolean loadIntent(Intent intent, boolean reuseIsolate) {
String action = intent.getAction();
if (Intent.ACTION_RUN.equals(action)) {
String route = intent.getStringExtra("route");
@@ -290,7 +346,7 @@ public final class FlutterActivityDelegate
if (route != null) {
flutterView.setInitialRoute(route);
}
- flutterView.runFromBundle(appBundlePath, intent.getStringExtra("snapshot"));
+ flutterView.runFromBundle(appBundlePath, intent.getStringExtra("snapshot"), "main", reuseIsolate);
return true;
}
@@ -476,5 +532,11 @@ public final class FlutterActivityDelegate
userLeaveHintListeners.add(listener);
return this;
}
+
+ @Override
+ public Registrar addViewDestroyListener(ViewDestroyListener listener) {
+ viewDestroyListeners.add(listener);
+ return this;
+ }
}
}
diff --git a/engine/src/flutter/shell/platform/android/io/flutter/app/FlutterApplication.java b/engine/src/flutter/shell/platform/android/io/flutter/app/FlutterApplication.java
index 39510bf27d..0651eb616e 100644
--- a/engine/src/flutter/shell/platform/android/io/flutter/app/FlutterApplication.java
+++ b/engine/src/flutter/shell/platform/android/io/flutter/app/FlutterApplication.java
@@ -4,6 +4,7 @@
package io.flutter.app;
+import android.app.Activity;
import android.app.Application;
import io.flutter.view.FlutterMain;
@@ -18,4 +19,12 @@ public class FlutterApplication extends Application {
super.onCreate();
FlutterMain.startInitialization(this);
}
+
+ private Activity mCurrentActivity = null;
+ public Activity getCurrentActivity() {
+ return mCurrentActivity;
+ }
+ public void setCurrentActivity(Activity mCurrentActivity) {
+ this.mCurrentActivity = mCurrentActivity;
+ }
}
diff --git a/engine/src/flutter/shell/platform/android/io/flutter/app/FlutterFragmentActivity.java b/engine/src/flutter/shell/platform/android/io/flutter/app/FlutterFragmentActivity.java
index 789424b917..3521ccc624 100644
--- a/engine/src/flutter/shell/platform/android/io/flutter/app/FlutterFragmentActivity.java
+++ b/engine/src/flutter/shell/platform/android/io/flutter/app/FlutterFragmentActivity.java
@@ -12,6 +12,7 @@ import android.support.v4.app.FragmentActivity;
import io.flutter.app.FlutterActivityDelegate.ViewFactory;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugin.common.PluginRegistry.Registrar;
+import io.flutter.view.FlutterNativeView;
import io.flutter.view.FlutterView;
/**
@@ -58,6 +59,11 @@ public class FlutterFragmentActivity
return null;
}
+ @Override
+ public FlutterNativeView createFlutterNativeView() {
+ return null;
+ }
+
@Override
public final boolean hasPlugin(String key) {
return pluginRegistry.hasPlugin(key);
diff --git a/engine/src/flutter/shell/platform/android/io/flutter/plugin/common/PluginRegistry.java b/engine/src/flutter/shell/platform/android/io/flutter/plugin/common/PluginRegistry.java
index 4586aea114..ac70be571f 100644
--- a/engine/src/flutter/shell/platform/android/io/flutter/plugin/common/PluginRegistry.java
+++ b/engine/src/flutter/shell/platform/android/io/flutter/plugin/common/PluginRegistry.java
@@ -6,6 +6,7 @@ package io.flutter.plugin.common;
import android.app.Activity;
import android.content.Intent;
+import io.flutter.view.FlutterNativeView;
import io.flutter.view.FlutterView;
import io.flutter.view.TextureRegistry;
@@ -140,6 +141,15 @@ public interface PluginRegistry {
* @return this {@link Registrar}.
*/
Registrar addUserLeaveHintListener(UserLeaveHintListener listener);
+
+ /**
+ * Adds a callback allowing the plugin to take part in handling incoming
+ * calls to {@link Activity#onDestroy()}.
+ *
+ * @param listener a {@link ViewDestroyListener} callback.
+ * @return this {@link Registrar}.
+ */
+ Registrar addViewDestroyListener(ViewDestroyListener listener);
}
/**
@@ -182,4 +192,13 @@ public interface PluginRegistry {
interface UserLeaveHintListener {
void onUserLeaveHint();
}
+
+ /**
+ * Delegate interface for handling an {@link Activity}'s onDestroy
+ * method being called. A plugin that implements this interface can
+ * adopt the FlutterNativeView by retaining a reference and returning true.
+ */
+ interface ViewDestroyListener {
+ boolean onViewDestroy(FlutterNativeView view);
+ }
}
diff --git a/engine/src/flutter/shell/platform/android/io/flutter/view/FlutterNativeView.java b/engine/src/flutter/shell/platform/android/io/flutter/view/FlutterNativeView.java
index 8be9a61ea1..9b1eb7af59 100644
--- a/engine/src/flutter/shell/platform/android/io/flutter/view/FlutterNativeView.java
+++ b/engine/src/flutter/shell/platform/android/io/flutter/view/FlutterNativeView.java
@@ -11,7 +11,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
import java.util.HashMap;
import java.util.Map;
-class FlutterNativeView implements BinaryMessenger {
+public class FlutterNativeView implements BinaryMessenger {
private static final String TAG = "FlutterNativeView";
private final Map mMessageHandlers;
@@ -21,23 +21,32 @@ class FlutterNativeView implements BinaryMessenger {
private long mNativePlatformView;
private FlutterView mFlutterView;
- FlutterNativeView(FlutterView flutterView) {
+ public FlutterNativeView(FlutterView flutterView) {
mFlutterView = flutterView;
attach(this);
assertAttached();
mMessageHandlers = new HashMap<>();
}
- FlutterNativeView() {
+ public FlutterNativeView() {
this(null);
}
+ public void detach() {
+ mFlutterView = null;
+ nativeDetach(mNativePlatformView);
+ }
+
public void destroy() {
mFlutterView = null;
nativeDestroy(mNativePlatformView);
mNativePlatformView = 0;
}
+ public void setFlutterView(FlutterView flutterView) {
+ mFlutterView = flutterView;
+ }
+
public boolean isAttached() {
return mNativePlatformView != 0;
}
@@ -168,6 +177,7 @@ class FlutterNativeView implements BinaryMessenger {
private static native long nativeAttach(FlutterNativeView view);
private static native void nativeDestroy(long nativePlatformViewAndroid);
+ private static native void nativeDetach(long nativePlatformViewAndroid);
private static native void nativeRunBundleAndSnapshot(long nativePlatformViewAndroid,
String bundlePath,
diff --git a/engine/src/flutter/shell/platform/android/io/flutter/view/FlutterView.java b/engine/src/flutter/shell/platform/android/io/flutter/view/FlutterView.java
index e491c8102d..d773fb048a 100644
--- a/engine/src/flutter/shell/platform/android/io/flutter/view/FlutterView.java
+++ b/engine/src/flutter/shell/platform/android/io/flutter/view/FlutterView.java
@@ -115,6 +115,10 @@ public class FlutterView extends SurfaceView
}
public FlutterView(Context context, AttributeSet attrs) {
+ this(context, attrs, null);
+ }
+
+ public FlutterView(Context context, AttributeSet attrs, FlutterNativeView nativeView) {
super(context, attrs);
mIsSoftwareRenderingEnabled = nativeGetIsSoftwareRenderingEnabled();
@@ -124,7 +128,12 @@ public class FlutterView extends SurfaceView
setFocusable(true);
setFocusableInTouchMode(true);
- mNativeView = new FlutterNativeView(this);
+ if (nativeView == null) {
+ mNativeView = new FlutterNativeView(this);
+ } else {
+ mNativeView = nativeView;
+ mNativeView.setFlutterView(this);
+ }
int color = 0xFF000000;
TypedValue typedValue = new TypedValue();
@@ -235,6 +244,10 @@ public class FlutterView extends SurfaceView
return super.onKeyDown(keyCode, event);
}
+ public FlutterNativeView getFlutterNativeView() {
+ return mNativeView;
+ }
+
public void addActivityLifecycleListener(ActivityLifecycleListener listener) {
mActivityLifecycleListeners.add(listener);
}
@@ -310,6 +323,20 @@ public class FlutterView extends SurfaceView
return mMetrics.devicePixelRatio;
}
+ public FlutterNativeView detach() {
+ if (!isAttached())
+ return null;
+ if (mDiscoveryReceiver != null) {
+ getContext().unregisterReceiver(mDiscoveryReceiver);
+ }
+ getHolder().removeCallback(mSurfaceCallback);
+ mNativeView.detach();
+
+ FlutterNativeView view = mNativeView;
+ mNativeView = null;
+ return view;
+ }
+
public void destroy() {
if (!isAttached())
return;
diff --git a/engine/src/flutter/shell/platform/android/platform_view_android_jni.cc b/engine/src/flutter/shell/platform/android/platform_view_android_jni.cc
index 83b026fff7..fa69dd3313 100644
--- a/engine/src/flutter/shell/platform/android/platform_view_android_jni.cc
+++ b/engine/src/flutter/shell/platform/android/platform_view_android_jni.cc
@@ -109,6 +109,10 @@ static jlong Attach(JNIEnv* env, jclass clazz, jobject flutterView) {
return reinterpret_cast(storage);
}
+static void Detach(JNIEnv* env, jobject jcaller, jlong platform_view) {
+ PLATFORM_VIEW->Detach();
+}
+
static void Destroy(JNIEnv* env, jobject jcaller, jlong platform_view) {
PLATFORM_VIEW->Detach();
delete &PLATFORM_VIEW;
@@ -328,6 +332,16 @@ bool PlatformViewAndroid::Register(JNIEnv* env) {
"(JLjava/lang/String;Ljava/lang/String;Ljava/lang/String;)V",
.fnPtr = reinterpret_cast(&shell::RunBundleAndSource),
},
+ {
+ .name = "nativeDetach",
+ .signature = "(J)V",
+ .fnPtr = reinterpret_cast(&shell::Detach),
+ },
+ {
+ .name = "nativeDestroy",
+ .signature = "(J)V",
+ .fnPtr = reinterpret_cast(&shell::Destroy),
+ },
{
.name = "nativeGetObservatoryUri",
.signature = "()Ljava/lang/String;",