Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit fcf1276

Browse files
committed
Flush all embedded Android view on hot restart.
Adds an OnEngineRestarted method to PlatformView, this is currently only implemented for Android where we need to use it for embedded views.
1 parent 4893b07 commit fcf1276

File tree

13 files changed

+77
-4
lines changed

13 files changed

+77
-4
lines changed

shell/common/engine.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ bool Engine::Restart(RunConfiguration configuration) {
103103
}
104104
runtime_controller_ = runtime_controller_->Clone();
105105
UpdateAssetManager(nullptr);
106+
delegate_.OnEngineRestart();
106107
return Run(std::move(configuration));
107108
}
108109

shell/common/engine.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ class Engine final : public blink::RuntimeDelegate {
3939
virtual void OnEngineHandlePlatformMessage(
4040
const Engine& engine,
4141
fml::RefPtr<blink::PlatformMessage> message) = 0;
42+
43+
virtual void OnEngineRestart() = 0;
4244
};
4345

4446
Engine(Delegate& delegate,

shell/common/platform_view.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ void PlatformView::HandlePlatformMessage(
8888
response->CompleteEmpty();
8989
}
9090

91+
void PlatformView::OnEngineRestart() const {}
92+
9193
void PlatformView::RegisterTexture(std::shared_ptr<flow::Texture> texture) {
9294
delegate_.OnPlatformViewRegisterTexture(*this, std::move(texture));
9395
}

shell/common/platform_view.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ class PlatformView {
108108
virtual void HandlePlatformMessage(
109109
fml::RefPtr<blink::PlatformMessage> message);
110110

111+
virtual void OnEngineRestart() const;
112+
111113
void SetNextFrameCallback(fml::closure closure);
112114

113115
void DispatchPointerDataPacket(

shell/common/shell.cc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,21 @@ void Shell::OnEngineHandlePlatformMessage(
740740
});
741741
}
742742

743+
// |shell::Engine::Delegate|
744+
void Shell::OnEngineRestart() {
745+
fml::AutoResetWaitableEvent latch;
746+
task_runners_.GetPlatformTaskRunner()->PostTask(
747+
[view = platform_view_->GetWeakPtr(), &latch] {
748+
if (view) {
749+
view->OnEngineRestart();
750+
}
751+
latch.Signal();
752+
});
753+
// This is blocking as any embedded platform views has to be flushed before
754+
// we re-run the Dart code.
755+
latch.Wait();
756+
}
757+
743758
// |blink::ServiceProtocol::Handler|
744759
fml::RefPtr<fml::TaskRunner> Shell::GetServiceProtocolHandlerTaskRunner(
745760
fml::StringView method) const {

shell/common/shell.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,9 @@ class Shell final : public PlatformView::Delegate,
195195
const Engine& engine,
196196
fml::RefPtr<blink::PlatformMessage> message) override;
197197

198+
// |shell::Engine::Delegate|
199+
void OnEngineRestart() override;
200+
198201
// |blink::ServiceProtocol::Handler|
199202
fml::RefPtr<fml::TaskRunner> GetServiceProtocolHandlerTaskRunner(
200203
fml::StringView method) const override;

shell/platform/android/io/flutter/app/FlutterPluginRegistry.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ public void detach() {
8282
mActivity = null;
8383
}
8484

85+
public void onEngineRestart() {
86+
mPlatformViewsController.onEngineRestart();
87+
}
88+
8589
private class FlutterRegistrar implements Registrar {
8690
private final String pluginKey;
8791

shell/platform/android/io/flutter/plugin/platform/PlatformViewsController.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,11 @@ public PlatformViewRegistry getRegistry() {
6969
}
7070

7171
public void onFlutterViewDestroyed() {
72-
for (VirtualDisplayController controller : vdControllers.values()) {
73-
controller.dispose();
74-
}
75-
vdControllers.clear();
72+
flushAllViews();
73+
}
74+
75+
public void onEngineRestart() {
76+
flushAllViews();
7677
}
7778

7879
@Override
@@ -297,4 +298,10 @@ private int toPhysicalPixels(double logicalPixels) {
297298
return (int) Math.round(logicalPixels * density);
298299
}
299300

301+
private void flushAllViews() {
302+
for (VirtualDisplayController controller : vdControllers.values()) {
303+
controller.dispose();
304+
}
305+
vdControllers.clear();
306+
}
300307
}

shell/platform/android/io/flutter/view/FlutterNativeView.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,14 @@ private void onFirstFrame() {
198198
mFlutterView.onFirstFrame();
199199
}
200200

201+
// Called by native to notify when the engine is restarted (cold reload).
202+
@SuppressWarnings("unused")
203+
private void onEngineRestart() {
204+
if (mPluginRegistry == null)
205+
return;
206+
mPluginRegistry.onEngineRestart();
207+
}
208+
201209
private static native long nativeAttach(FlutterNativeView view);
202210
private static native void nativeDestroy(long nativePlatformViewAndroid);
203211
private static native void nativeDetach(long nativePlatformViewAndroid);

shell/platform/android/platform_view_android.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,17 @@ void PlatformViewAndroid::HandlePlatformMessage(
157157
}
158158
}
159159

160+
// |shell::PlatformView|
161+
void PlatformViewAndroid::OnEngineRestart() const {
162+
JNIEnv* env = fml::jni::AttachCurrentThread();
163+
fml::jni::ScopedJavaLocalRef<jobject> view = java_object_.get(env);
164+
if (view.is_null()) {
165+
// The Java object died.
166+
return;
167+
}
168+
FlutterViewOnEngineRestart(fml::jni::AttachCurrentThread(), view.obj());
169+
}
170+
160171
void PlatformViewAndroid::DispatchSemanticsAction(JNIEnv* env,
161172
jint id,
162173
jint action,

0 commit comments

Comments
 (0)