Skip to content

Commit 90ca7d8

Browse files
authored
fix(android): activity recreation crashes with tabs and complex view hierarchies (#11223)
1 parent b78fcdd commit 90ca7d8

2 files changed

Lines changed: 28 additions & 1 deletion

File tree

packages/core/ui/action-bar/action-bar-common.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ export class ActionBarBase extends View implements ActionBarDefinition {
2929
public effectiveContentInsetRight: number;
3030

3131
disposeNativeView() {
32-
this._actionItems = null;
3332
super.disposeNativeView();
3433
}
3534

packages/core/ui/frame/index.android.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,34 @@ export class ActivityCallbacksImplementation implements AndroidActivityCallbacks
814814
const isRestart = !!savedInstanceState && moduleLoaded;
815815
superFunc.call(activity, isRestart ? savedInstanceState : null);
816816

817+
if (isRestart && activity.getSupportFragmentManager) {
818+
// Remove restored fragments that NativeScript will recreate via _setupUI/createNativeView.
819+
// NativeScript tears down and rebuilds the entire view tree on activity recreation,
820+
// so restored fragments (especially from ViewPager2/tabs) become orphaned without views.
821+
// NativeScript core's own fragments use tags like "fragment{id}[{depth}]" and are
822+
// handled by _processNextNavigationEntry. We remove all non-NativeScript fragments.
823+
const fm = activity.getSupportFragmentManager();
824+
const fragments = fm.getFragments();
825+
const size = fragments?.size?.() ?? 0;
826+
if (size > 0) {
827+
const ft = fm.beginTransaction();
828+
let removed = false;
829+
for (let i = size - 1; i >= 0; i--) {
830+
const f = fragments.get(i);
831+
if (!f) continue;
832+
const tag = f.getTag();
833+
if (tag && tag.startsWith('fragment')) {
834+
continue;
835+
}
836+
ft.remove(f);
837+
removed = true;
838+
}
839+
if (removed) {
840+
ft.commitNowAllowingStateLoss();
841+
}
842+
}
843+
}
844+
817845
// Try to get the rootViewId form the saved state in case the activity
818846
// was destroyed and we are now recreating it.
819847
if (savedInstanceState) {

0 commit comments

Comments
 (0)