1414import android .util .SparseArray ;
1515import android .view .MotionEvent ;
1616import android .view .View ;
17+ import android .view .ViewGroup ;
1718import android .widget .FrameLayout ;
1819import androidx .annotation .NonNull ;
1920import androidx .annotation .Nullable ;
@@ -83,7 +84,7 @@ public class PlatformViewsController implements PlatformViewsAccessibilityDelega
8384 // The views returned by `PlatformView#getView()`.
8485 //
8586 // This only applies to hybrid composition.
86- private final SparseArray <View > platformViews ;
87+ private final SparseArray <PlatformView > platformViews ;
8788
8889 // The platform view parents that are appended to `FlutterView`.
8990 // If an entry in `platformViews` doesn't have an entry in this array, the platform view isn't
@@ -143,32 +144,24 @@ public void createAndroidViewForPlatformView(
143144 }
144145
145146 final PlatformView platformView = factory .create (context , request .viewId , createParams );
146- final View view = platformView .getView ();
147- if (view == null ) {
148- throw new IllegalStateException (
149- "PlatformView#getView() returned null, but an Android view reference was expected." );
150- }
151- if (view .getParent () != null ) {
152- throw new IllegalStateException (
153- "The Android view returned from PlatformView#getView() was already added to a parent view." );
154- }
155- platformViews .put (request .viewId , view );
147+ platformViews .put (request .viewId , platformView );
156148 }
157149
158150 @ Override
159151 public void disposeAndroidViewForPlatformView (int viewId ) {
160152 // Hybrid view.
161- final View platformView = platformViews .get (viewId );
153+ final PlatformView platformView = platformViews .get (viewId );
162154 final FlutterMutatorView parentView = platformViewParent .get (viewId );
163155 if (platformView != null ) {
164156 if (parentView != null ) {
165- parentView .removeView (platformView );
157+ parentView .removeView (platformView . getView () );
166158 }
167159 platformViews .remove (viewId );
160+ platformView .dispose ();
168161 }
169162
170163 if (parentView != null ) {
171- ((FlutterView ) flutterView ).removeView (parentView );
164+ ((ViewGroup ) parentView . getParent () ).removeView (parentView );
172165 platformViewParent .remove (viewId );
173166 }
174167 }
@@ -311,8 +304,10 @@ public void onTouch(@NonNull PlatformViewsChannel.PlatformViewTouch touch) {
311304 vdControllers .get (touch .viewId ).dispatchTouchEvent (event );
312305 } else if (platformViews .get (viewId ) != null ) {
313306 final MotionEvent event = toMotionEvent (density , touch , /*usingVirtualDiplays=*/ false );
314- View view = platformViews .get (touch .viewId );
315- view .dispatchTouchEvent (event );
307+ View view = platformViews .get (touch .viewId ).getView ();
308+ if (view != null ) {
309+ view .dispatchTouchEvent (event );
310+ }
316311 } else {
317312 throw new IllegalStateException ("Sending touch to an unknown view with id: " + viewId );
318313 }
@@ -580,7 +575,7 @@ public void onPreEngineRestart() {
580575 public View getPlatformViewById (Integer id ) {
581576 // Hybrid composition.
582577 if (platformViews .get (id ) != null ) {
583- return platformViews .get (id );
578+ return platformViews .get (id ). getView () ;
584579 }
585580 VirtualDisplayController controller = vdControllers .get (id );
586581 if (controller == null ) {
@@ -690,6 +685,10 @@ private void flushAllViews() {
690685 controller .dispose ();
691686 }
692687 vdControllers .clear ();
688+
689+ while (platformViews .size () > 0 ) {
690+ channelHandler .disposeAndroidViewForPlatformView (platformViews .keyAt (0 ));
691+ }
693692 }
694693
695694 private void initializeRootImageViewIfNeeded () {
@@ -701,19 +700,27 @@ private void initializeRootImageViewIfNeeded() {
701700
702701 @ VisibleForTesting
703702 void initializePlatformViewIfNeeded (int viewId ) {
704- final View view = platformViews .get (viewId );
705- if (view == null ) {
703+ final PlatformView platformView = platformViews .get (viewId );
704+ if (platformView == null ) {
706705 throw new IllegalStateException (
707706 "Platform view hasn't been initialized from the platform view channel." );
708707 }
709708 if (platformViewParent .get (viewId ) != null ) {
710709 return ;
711710 }
711+ if (platformView .getView () == null ) {
712+ throw new IllegalStateException (
713+ "PlatformView#getView() returned null, but an Android view reference was expected." );
714+ }
715+ if (platformView .getView ().getParent () != null ) {
716+ throw new IllegalStateException (
717+ "The Android view returned from PlatformView#getView() was already added to a parent view." );
718+ }
712719 final FlutterMutatorView parentView =
713720 new FlutterMutatorView (
714721 context , context .getResources ().getDisplayMetrics ().density , androidTouchProcessor );
715722 platformViewParent .put (viewId , parentView );
716- parentView .addView (view );
723+ parentView .addView (platformView . getView () );
717724 ((FlutterView ) flutterView ).addView (parentView );
718725 }
719726
@@ -740,9 +747,11 @@ public void onDisplayPlatformView(
740747
741748 final FrameLayout .LayoutParams layoutParams =
742749 new FrameLayout .LayoutParams (viewWidth , viewHeight );
743- final View platformView = platformViews .get (viewId );
744- platformView .setLayoutParams (layoutParams );
745- platformView .bringToFront ();
750+ final View view = platformViews .get (viewId ).getView ();
751+ if (view != null ) {
752+ view .setLayoutParams (layoutParams );
753+ view .bringToFront ();
754+ }
746755 currentFrameUsedPlatformViewIds .add (viewId );
747756 }
748757
0 commit comments