@@ -834,6 +834,24 @@ class FragmentCallbacksImplementation implements AndroidFragmentCallbacks {
834834 entry . viewSavedState = null ;
835835 }
836836
837+ // fixes 'java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first'.
838+ // on app resume in nested frame scenarios with support library version greater than 26.0.0
839+ // HACK: this whole code block shouldn't be necessary as the native view is supposedly removed from its parent
840+ // right after onDestroyView(...) is called but for some reason the fragment view (page) still thinks it has a
841+ // parent while its supposed parent believes it properly removed its children; in order to "force" the child to
842+ // lose its parent we temporarily add it to the parent, and then remove it (addViewInLayout doesn't trigger layout pass)
843+ const nativeView = page . nativeViewProtected ;
844+ if ( nativeView != null ) {
845+ const parentView = nativeView . getParent ( ) ;
846+ if ( parentView instanceof android . view . ViewGroup ) {
847+ if ( parentView . getChildCount ( ) === 0 ) {
848+ parentView . addViewInLayout ( nativeView , - 1 , new org . nativescript . widgets . CommonLayoutParams ( ) ) ;
849+ }
850+
851+ parentView . removeView ( nativeView ) ;
852+ }
853+ }
854+
837855 return page . nativeViewProtected ;
838856 }
839857
@@ -873,24 +891,6 @@ class FragmentCallbacksImplementation implements AndroidFragmentCallbacks {
873891 traceError ( `${ fragment } .onDestroy: entry has no resolvedPage` ) ;
874892 return null ;
875893 }
876-
877- // fixes 'java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first'.
878- // on app resume in nested frame scenarios with support library version greater than 26.0.0
879- // HACK: this whole code block shouldn't be necessary as the native view is supposedly removed from its parent
880- // right after onDestroyView(...) is called but for some reason the fragment view (page) still thinks it has a
881- // parent while its supposed parent believes it properly removed its children; in order to "force" the child to
882- // lose its parent we temporarily add it to the parent, and then remove it (addViewInLayout doesn't trigger layout pass)
883- const nativeView = page . nativeViewProtected ;
884- if ( nativeView != null ) {
885- const parentView = nativeView . getParent ( ) ;
886- if ( parentView instanceof android . view . ViewGroup ) {
887- if ( parentView . getChildCount ( ) === 0 ) {
888- parentView . addViewInLayout ( nativeView , - 1 , new org . nativescript . widgets . CommonLayoutParams ( ) ) ;
889- }
890-
891- parentView . removeView ( nativeView ) ;
892- }
893- }
894894 }
895895
896896 @profile
0 commit comments