You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fix(android): Fix Activity memory leak in ScreenDummyLayoutHelper due to unreleased view references and missing lifecycle cleanup (#3638)
## Description
Fix Activity memory leak in `ScreenDummyLayoutHelper` caused by
unreleased view references (`CoordinatorLayout`, `AppBarLayout`,
`Toolbar`, `View`) when the host Activity is destroyed.
The helper created dummy views using Activity-scoped `Context` to
measure header heights, but never released these references on Activity
destruction. This created a strong reference chain
(`ScreenDummyLayoutHelper → Views → ContextThemeWrapper → Activity`)
that prevents GC of destroyed Activities.
Closes: #3636
## Changes
- Changed view fields from `lateinit var` to nullable `var? = null` to
allow reference release
- Always register `LifecycleEventListener` in constructor (regardless of
init success)
- Keep listener registered across Activity lifecycle (never remove it)
to ensure `onHostDestroy` is always called
- Release all view references, reset cache, and reset
`isLayoutInitialized` in `onHostDestroy` to break the reference chain
and allow re-initialization on next `onHostResume`
- Wrap `computeDummyLayout` in `synchronized(this)` block with null-safe
local variables, returning `0.0f` as fallback when views are already
cleaned up
- Synchronize `onHostDestroy` cleanup on the same monitor to prevent
race conditions
## Screenshots / GIFs
### Before
https://github.com/user-attachments/assets/b22c935d-9e22-4ddb-92f5-91be5caee430
### After
https://github.com/user-attachments/assets/c5946a30-3537-46a0-a15d-33183823294b
## Test plan
<!--
Please name all newly added and existing test files that you tested the
changes with.
This section should also contain short description of steps to reproduce
the issue.
The reproduction code should be minimal & complete. Don't exclude
exports or remove "not important" parts of reproduction example.
-->
1. Launch a React Native app with native stack navigation using
`react-native-screens`
2. Navigate through screens to trigger `ScreenDummyLayoutHelper`
initialization
3. Trigger Activity recreation (dev reload, configuration change, or
process restoration)
4. Use Android Studio Memory Profiler or LeakCanary to verify the
previous Activity is no longer retained
5. Repeat to confirm no leak accumulation across multiple Activity
recreations
6. Verify header height computation still works correctly after Activity
recreation (headers render with correct dimensions)
## Checklist
- [X] Included code example that can be used to test this change.
- [X] Updated / created local changelog entries in relevant test files.
- [ ] For visual changes, included screenshots / GIFs / recordings
documenting the change.
- [ ] For API changes, updated relevant public types.
- [ ] Ensured that CI passes
---------
Co-authored-by: Tomasz Boroń <tomasz.boron@swmansion.com>
0 commit comments