Allow FlutterEngine to be used on back-to-back screens (#31264). (flutter/engine#8808)
This commit is contained in:
@@ -378,25 +378,6 @@ public class FlutterFragment extends Fragment {
|
||||
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
|
||||
flutterView = new FlutterView(getContext(), getRenderMode(), getTransparencyMode());
|
||||
flutterView.addOnFirstFrameRenderedListener(onFirstFrameRenderedListener);
|
||||
|
||||
// We post() the code that attaches the FlutterEngine to our FlutterView because there is
|
||||
// some kind of blocking logic on the native side when the surface is connected. That lag
|
||||
// causes launching Activitys to wait a second or two before launching. By post()'ing this
|
||||
// behavior we are able to move this blocking logic to after the Activity's launch.
|
||||
// TODO(mattcarroll): figure out how to avoid blocking the MAIN thread when connecting a surface
|
||||
new Handler().post(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
flutterView.attachToFlutterEngine(flutterEngine);
|
||||
|
||||
// TODO(mattcarroll): the following call should exist here, but the plugin system needs to be revamped.
|
||||
// The existing attach() method does not know how to handle this kind of FlutterView.
|
||||
//flutterEngine.getPluginRegistry().attach(this, getActivity());
|
||||
|
||||
doInitialFlutterViewRun();
|
||||
}
|
||||
});
|
||||
|
||||
return flutterView;
|
||||
}
|
||||
|
||||
@@ -486,9 +467,34 @@ public class FlutterFragment extends Fragment {
|
||||
return FlutterView.TransparencyMode.valueOf(transparencyModeName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStart() {
|
||||
super.onStart();
|
||||
Log.d(TAG, "onStart()");
|
||||
|
||||
// We post() the code that attaches the FlutterEngine to our FlutterView because there is
|
||||
// some kind of blocking logic on the native side when the surface is connected. That lag
|
||||
// causes launching Activitys to wait a second or two before launching. By post()'ing this
|
||||
// behavior we are able to move this blocking logic to after the Activity's launch.
|
||||
// TODO(mattcarroll): figure out how to avoid blocking the MAIN thread when connecting a surface
|
||||
new Handler().post(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
flutterView.attachToFlutterEngine(flutterEngine);
|
||||
|
||||
// TODO(mattcarroll): the following call should exist here, but the plugin system needs to be revamped.
|
||||
// The existing attach() method does not know how to handle this kind of FlutterView.
|
||||
//flutterEngine.getPluginRegistry().attach(this, getActivity());
|
||||
|
||||
doInitialFlutterViewRun();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
Log.d(TAG, "onResume()");
|
||||
flutterEngine.getLifecycleChannel().appIsResumed();
|
||||
}
|
||||
|
||||
@@ -517,6 +523,7 @@ public class FlutterFragment extends Fragment {
|
||||
super.onStop();
|
||||
Log.d(TAG, "onStop()");
|
||||
flutterEngine.getLifecycleChannel().appIsPaused();
|
||||
flutterView.detachFromFlutterEngine();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -524,7 +531,6 @@ public class FlutterFragment extends Fragment {
|
||||
super.onDestroyView();
|
||||
Log.d(TAG, "onDestroyView()");
|
||||
flutterView.removeOnFirstFrameRenderedListener(onFirstFrameRenderedListener);
|
||||
flutterView.detachFromFlutterEngine();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -547,7 +547,7 @@ public class FlutterView extends FrameLayout {
|
||||
}
|
||||
|
||||
private boolean isAttachedToFlutterEngine() {
|
||||
return flutterEngine != null;
|
||||
return flutterEngine != null && flutterEngine.getRenderer().isAttachedTo(renderSurface);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -10,6 +10,7 @@ import android.graphics.SurfaceTexture;
|
||||
import android.os.Build;
|
||||
import android.os.Handler;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.view.Surface;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
@@ -43,6 +44,14 @@ public class FlutterRenderer implements TextureRegistry {
|
||||
this.flutterJNI = flutterJNI;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if this {@code FlutterRenderer} is attached to the given {@link RenderSurface},
|
||||
* false otherwise.
|
||||
*/
|
||||
public boolean isAttachedTo(@NonNull RenderSurface renderSurface) {
|
||||
return this.renderSurface == renderSurface;
|
||||
}
|
||||
|
||||
public void attachToRenderSurface(@NonNull RenderSurface renderSurface) {
|
||||
// TODO(mattcarroll): determine desired behavior when attaching to an already attached renderer
|
||||
if (this.renderSurface != null) {
|
||||
|
||||
Reference in New Issue
Block a user