Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 41ba93d

Browse files
authored
fix(android): IllegalStateException with tabview&nested frames (NativeScript#6495)
1 parent c41e3e0 commit 41ba93d

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

tns-core-modules/ui/frame/frame.android.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -859,7 +859,38 @@ class FragmentCallbacksImplementation implements AndroidFragmentCallbacks {
859859
if (traceEnabled()) {
860860
traceWrite(`${fragment}.onDestroy()`, traceCategories.NativeLifecycle);
861861
}
862+
862863
superFunc.call(fragment);
864+
865+
const entry = this.entry;
866+
if (!entry) {
867+
traceError(`${fragment}.onDestroy: entry is null or undefined`);
868+
return null;
869+
}
870+
871+
const page = entry.resolvedPage;
872+
if (!page) {
873+
traceError(`${fragment}.onDestroy: entry has no resolvedPage`);
874+
return null;
875+
}
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+
}
863894
}
864895

865896
@profile

0 commit comments

Comments
 (0)