fix: activity recreation crashes with tabs and complex view hierarchies#11223
Open
edusperoni wants to merge 1 commit into
Open
fix: activity recreation crashes with tabs and complex view hierarchies#11223edusperoni wants to merge 1 commit into
edusperoni wants to merge 1 commit into
Conversation
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.
a6ca6be to
f954135
Compare
|
View your CI Pipeline Execution ↗ for commit f954135
☁️ Nx Cloud last updated this comment at |
farfromrefug
approved these changes
May 20, 2026
CatchABus
requested changes
May 20, 2026
| // handled by _processNextNavigationEntry. We remove all non-NativeScript fragments. | ||
| const fm = activity.getSupportFragmentManager(); | ||
| const fragments = fm.getFragments(); | ||
| if (fragments && fragments.size() > 0) { |
Contributor
There was a problem hiding this comment.
The java size() call can be done only once and store the return value in a variable.
e.g.
const size = fragments.size();
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
ActionBar's disposeNativeView() nulled _actionItems, causing
"Cannot read properties of null (reading 'addItem')" when the action bar
was recreated during _setupUI after activity destruction.
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.