Skip to content

Commit 4fc95eb

Browse files
Fixed memory leaks within FlutterFragment and FlutterView (flutter#34268, flutter#34269, flutter#34270). (flutter#9288)
1 parent de350c4 commit 4fc95eb

File tree

4 files changed

+18
-3
lines changed

4 files changed

+18
-3
lines changed

shell/platform/android/io/flutter/embedding/android/FlutterFragment.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,7 @@ public void onDetach() {
607607

608608
// Null out the platformPlugin to avoid a possible retain cycle between the plugin, this Fragment,
609609
// and this Fragment's Activity.
610+
platformPlugin.destroy();
610611
platformPlugin = null;
611612

612613
// Destroy our FlutterEngine if we're not set to retain it.

shell/platform/android/io/flutter/embedding/android/FlutterView.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,11 +561,16 @@ public void detachFromFlutterEngine() {
561561
}
562562
Log.d(TAG, "Detaching from Flutter Engine");
563563

564+
// Disconnect and clean up the AccessibilityBridge.
565+
accessibilityBridge.release();
566+
accessibilityBridge = null;
567+
564568
// Inform the Android framework that it should retrieve a new InputConnection
565569
// now that the engine is detached. The new InputConnection will be null, which
566570
// signifies that this View does not process input (until a new engine is attached).
567571
// TODO(mattcarroll): once this is proven to work, move this line ot TextInputPlugin
568572
textInputPlugin.getInputMethodManager().restartInput(this);
573+
textInputPlugin.destroy();
569574

570575
// Instruct our FlutterRenderer that we are no longer interested in being its RenderSurface.
571576
didRenderFirstFrame = false;

shell/platform/android/io/flutter/plugin/editing/TextInputPlugin.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public class TextInputPlugin {
4646
// target is a platform view. See the comments on lockPlatformViewInputConnection for more details.
4747
private boolean isInputConnectionLocked;
4848

49-
// TODO(mattcarroll): change @Nullable to @NonNull once new embedding integrates PlatformViewsController.
49+
// TODO(mattcarroll): change @Nullable to @NonNull once new embedding integrates PlatformViewsController (#34286).
5050
public TextInputPlugin(View view, @NonNull DartExecutor dartExecutor, @Nullable PlatformViewsController platformViewsController) {
5151
mView = view;
5252
mImm = (InputMethodManager) view.getContext().getSystemService(
@@ -85,7 +85,7 @@ public void clearClient() {
8585
}
8686
});
8787
this.platformViewsController = platformViewsController;
88-
// TODO(mattcarroll): remove if-statement once new embedding integrates PlatformViewsController.
88+
// TODO(mattcarroll): remove if-statement once new embedding integrates PlatformViewsController (#34286).
8989
if (platformViewsController != null) {
9090
platformViewsController.attachTextInputPlugin(this);
9191
}
@@ -127,7 +127,7 @@ public void unlockPlatformViewInputConnection() {
127127
* The TextInputPlugin instance should not be used after calling this.
128128
*/
129129
public void destroy() {
130-
// TODO(mattcarroll): Remove if-statement once new embedding integrates PlatformViewsController.
130+
// TODO(mattcarroll): Remove if-statement once new embedding integrates PlatformViewsController (#34286).
131131
if (platformViewsController != null) {
132132
platformViewsController.detachTextInputPlugin();
133133
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,15 @@ public PlatformPlugin(Activity activity, PlatformChannel platformChannel) {
9494
mEnabledOverlays = DEFAULT_SYSTEM_UI;
9595
}
9696

97+
/**
98+
* Releases all resources held by this {@code PlatformPlugin}.
99+
* <p>
100+
* Do not invoke any methods on a {@code PlatformPlugin} after invoking this method.
101+
*/
102+
public void destroy() {
103+
this.platformChannel.setPlatformMessageHandler(null);
104+
}
105+
97106
private void playSystemSound(PlatformChannel.SoundType soundType) {
98107
if (soundType == PlatformChannel.SoundType.CLICK) {
99108
View view = activity.getWindow().getDecorView();

0 commit comments

Comments
 (0)