Skip to content

fix: activity recreation crashes with tabs and complex view hierarchies#11223

Open
edusperoni wants to merge 1 commit into
mainfrom
fix/remove-unknown-fragments-on-restore
Open

fix: activity recreation crashes with tabs and complex view hierarchies#11223
edusperoni wants to merge 1 commit into
mainfrom
fix/remove-unknown-fragments-on-restore

Conversation

@edusperoni
Copy link
Copy Markdown
Contributor

PR Checklist

What is the current behavior?

When "Don't keep activities" is enabled (or the system destroys the activity
to reclaim memory), NativeScript apps using ViewPager2-based tabs (e.g.
@nativescript-community/ui-material-tabs) crash on foregrounding.

What is the new behavior?

Two root causes are addressed:

  1. ActionBar's disposeNativeView() nulled _actionItems, causing
    "Cannot read properties of null (reading 'addItem')" when the action bar
    was recreated during _setupUI after activity destruction.

  2. Android's FragmentManager restores all fragments from savedInstanceState
    during super.onCreate(), including third-party fragments (ViewPager2's
    f0, f1, etc.). NativeScript's _tearDownUI/_setupUI cycle rebuilds the
    entire view tree from scratch, leaving these restored fragments orphaned
    — they reference container views that no longer exist, causing "Fragment
    does not have a view" errors. We now remove non-NativeScript fragments
    immediately after super.onCreate() while preserving core's own fragments
    (tagged fragment{id}[{depth}]) which have a proper restoration path via
    _processNextNavigationEntry and FragmentCallbacksImplementation.onCreateView.

When "Don't keep activities" is enabled (or the system destroys the activity
to reclaim memory), NativeScript apps using ViewPager2-based tabs (e.g.
@nativescript-community/ui-material-tabs) crash on foregrounding.

Two root causes are addressed:

1. ActionBar's disposeNativeView() nulled _actionItems, causing
   "Cannot read properties of null (reading 'addItem')" when the action bar
   was recreated during _setupUI after activity destruction.

2. Android's FragmentManager restores all fragments from savedInstanceState
   during super.onCreate(), including third-party fragments (ViewPager2's
   f0, f1, etc.). NativeScript's _tearDownUI/_setupUI cycle rebuilds the
   entire view tree from scratch, leaving these restored fragments orphaned
   — they reference container views that no longer exist, causing "Fragment
   does not have a view" errors. We now remove non-NativeScript fragments
   immediately after super.onCreate() while preserving core's own fragments
   (tagged fragment{id}[{depth}]) which have a proper restoration path via
   _processNextNavigationEntry and FragmentCallbacksImplementation.onCreateView.
@edusperoni edusperoni force-pushed the fix/remove-unknown-fragments-on-restore branch from a6ca6be to f954135 Compare May 20, 2026 02:34
@nx-cloud
Copy link
Copy Markdown

nx-cloud Bot commented May 20, 2026

View your CI Pipeline Execution ↗ for commit f954135

Command Status Duration Result
nx test apps-automated -c=android ✅ Succeeded 3m 57s View ↗
nx run-many --target=test --configuration=ci --... ✅ Succeeded <1s View ↗

☁️ Nx Cloud last updated this comment at 2026-05-20 02:42:08 UTC

// handled by _processNextNavigationEntry. We remove all non-NativeScript fragments.
const fm = activity.getSupportFragmentManager();
const fragments = fm.getFragments();
if (fragments && fragments.size() > 0) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The java size() call can be done only once and store the return value in a variable.
e.g.

const size = fragments.size();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants