fix(android): keep the fragment snapshot out of the cached default background by anjak03 · Pull Request #11679 · NativeScript/NativeScript
Repository navigation
fix(android): keep the fragment snapshot out of the cached default background - #11679
Open
anjak03 wants to merge 3 commits into NativeScript:main from anjak03:fix/snapshot-cached-background
Open
fix(android): keep the fragment snapshot out of the cached default background #11679 anjak03 wants to merge 3 commits into NativeScript:main from anjak03:fix/snapshot-cached-background
Conversation
anjak03 commented Nov 25, 2026 · edited Loading
Copy link Copy Markdown
Contributor
PR Checklist
- The PR title follows our guidelines: https://github.com/NativeScript/NativeScript/blob/main/tools/notes/CONTRIBUTING.md#commit-messages .
- There is an issue for the bug/feature this PR is for. To avoid wasting your time, it's best to open a suggestion issue first and wait for approval before working on it.
- You have read and signed the CLA .
- All existing tests are passing: https://github.com/NativeScript/NativeScript/blob/main/tools/notes/DevelopmentWorkflow.md#unit-testing .
- Tests for the changes are included - https://github.com/NativeScript/NativeScript/blob/main/tools/notes/WritingUnitTests.md .
What is the current behavior?
When the parent fragment of a Frame or TabView is removed, onDestroyView puts a BitmapDrawable snapshot of the content on the native view as its background and stores _originalBackground. On the next onLoaded, backgroundColor = null runs onBackgroundOrBorderPropertyChanged, which caches nativeView.getBackground() as _cachedDrawable while it is still undefined. At that moment the native background is the snapshot, so the snapshot becomes the view's default background. On a later removal and load, backgroundColor = null restores that default. With markingMode: 'none' the Java BitmapDrawable behind it has been collected by then, so _applyBackground throws Attempt to use cleared object reference, the load stops and the page stays blank.
What is the new behavior?
Before onDestroyView puts the snapshot on the native view, Frame and TabView cache the current native background as _cachedDrawable if it is still undefined. The snapshot never becomes the default background, so restoring the background on load works on every removal and load cycle.
Verified on an Android 16 emulator with a production Angular app built against @nativescript/core 9.1.4 with this change: 15 rounds of leaving a page with a TabView and nested frames and going back had no "cleared object reference" error and no blank screen. Without the change the same rounds gave 4 blank screens out of 15.
Fixes #11678 .
fix(android): keep the fragment snapshot out of the cached default ba...
65a4056
...ckgroundNathanWalker approved these changes Nov 25, 2026
View reviewed changes
NathanWalker left a comment
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 .
Thanks for the clear write-up and the emulator numbers. The root cause checks out: _cachedDrawable is only written in onBackgroundOrBorderPropertyChanged (ui/core/view/index.android.ts), which caches getBackground() while it's undefined. The onDestroyView snapshot goes on through raw setBackground/setBackgroundDrawable, skipping that, so the next backgroundColor = null caches the snapshot as the default. After _originalBackground replaces it, only the JS expando holds the BitmapDrawable, and under markingMode: 'none' it gets collected.
Seeding here captures the right default in every case I could find:
- If a CSS background was already applied, the cache is already set, so this does nothing.
- A color set then cleared is cached at the first set, so this does nothing.
- The common case, where
ContentLayout/GridLayouthave no default background, seedsnull, which=== undefinedcorrectly treats as set.
These two onDestroyView blocks are the only places that put a temporary drawable on a view background, so the coverage is complete.
Verified: core vitest 680 passed / 1 skipped, tsc and prettier clean.
One request, non-blocking: a unit spec for the Frame side would be cheap. Following activity-callbacks.android.spec.ts, mock ./fragment.transitions and getNativeApp from ../../application/helpers-common, stub android.graphics.drawable.BitmapDrawable, give it a fake native view, and call new FragmentCallbacksImplementation().onDestroyView({ getRemovingParentFragment: () => ({}) }, superFn). Assert that _cachedDrawable is null (the pre-snapshot background) and that the background is now the snapshot. That fails on main ("expected undefined to be null") and passes here. Also assert that an existing _cachedDrawable is left alone. The TabView side is harder to reach (its fragment class is created inside initializeNativeClasses()), so the Frame spec alone is fine.
Outdated
| if (nativeFrameView) { | ||
| const bitmapDrawable = new android.graphics.drawable.BitmapDrawable(getNativeApp<android.app.Application>().getApplicationContext().getResources(), this.backgroundBitmap); | ||
| this.frame._originalBackground = this.frame.backgroundColor || new Color('White'); | ||
| // the default background is cached on the first background change, which must not be this snapshot |
Outdated
| const bitmapDrawable = new android.graphics.drawable.BitmapDrawable(appResources, this.backgroundBitmap); | ||
| this.owner._originalBackground = this.owner.backgroundColor || new Color('White'); | ||
| this.owner.nativeViewProtected.setBackground(bitmapDrawable); | ||
| // the default background is cached on the first background change, which must not be this snapshot |
test(android): cover the snapshot default background seed
db7f0c0
nx-cloud
Bot
commented
Nov 26, 2026
·
edited
Loading
Copy link Copy Markdown
|
View your CI Pipeline Execution ↗ for commit e167eb9
💡 With Ultracache enabled, Nx Cloud learns each task's file inputs in CI and uses them on later runs. Turn on Ultracache in workspace settings ↗ · Read docs ↗ ☁️ Nx Cloud last updated this comment at |
pkg-pr-new
Bot
commented
Nov 26, 2026
·
edited
Loading
Copy link Copy Markdown
commit: |
Merge branch 'main' into fix/snapshot-cached-background
e167eb9
This branch has not been deployed
No deployments
Sign up for free to join this conversation on GitHub . Already have an account? Sign in to comment