From 7d21b5c9ab1372e5a689c92fde337184b02876dd Mon Sep 17 00:00:00 2001 From: Manol Donev Date: Mon, 5 Nov 2018 16:17:51 +0200 Subject: [PATCH] fix(android): IllegalStateException with tabview&nested frames (#6495) --- tns-core-modules/ui/frame/frame.android.ts | 31 ++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/tns-core-modules/ui/frame/frame.android.ts b/tns-core-modules/ui/frame/frame.android.ts index 3f98a8ef0..81d6cf7fc 100644 --- a/tns-core-modules/ui/frame/frame.android.ts +++ b/tns-core-modules/ui/frame/frame.android.ts @@ -859,7 +859,38 @@ class FragmentCallbacksImplementation implements AndroidFragmentCallbacks { if (traceEnabled()) { traceWrite(`${fragment}.onDestroy()`, traceCategories.NativeLifecycle); } + superFunc.call(fragment); + + const entry = this.entry; + if (!entry) { + traceError(`${fragment}.onDestroy: entry is null or undefined`); + return null; + } + + const page = entry.resolvedPage; + if (!page) { + traceError(`${fragment}.onDestroy: entry has no resolvedPage`); + return null; + } + + // fixes 'java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first'. + // on app resume in nested frame scenarios with support library version greater than 26.0.0 + // HACK: this whole code block shouldn't be necessary as the native view is supposedly removed from its parent + // right after onDestroyView(...) is called but for some reason the fragment view (page) still thinks it has a + // parent while its supposed parent believes it properly removed its children; in order to "force" the child to + // lose its parent we temporarily add it to the parent, and then remove it (addViewInLayout doesn't trigger layout pass) + const nativeView = page.nativeViewProtected; + if (nativeView != null) { + const parentView = nativeView.getParent(); + if (parentView instanceof android.view.ViewGroup) { + if (parentView.getChildCount() === 0) { + parentView.addViewInLayout(nativeView, -1, new org.nativescript.widgets.CommonLayoutParams()); + } + + parentView.removeView(nativeView); + } + } } @profile