Skip to content

Commit

Permalink
fix(android): IllegalStateException with tabview&nested frames (#6495)
Browse files Browse the repository at this point in the history
  • Loading branch information
manoldonev authored and SvetoslavTsenov committed Nov 7, 2018
1 parent b6a5df1 commit 7d21b5c
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions tns-core-modules/ui/frame/frame.android.ts
Expand Up @@ -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
Expand Down

0 comments on commit 7d21b5c

Please sign in to comment.