Skip to content

Commit 8393ee2

Browse files
authored
fix: memory leak on Android (#1343)
## 📜 Description Improving memory leak behavior on Android. ## 💡 Motivation and Context As described in #1334 - it doesn't fully solves memory leaks when activity restarts, but it resolves some other issues that could cause memory leak further. There is no breaking changes, so it's safe to have these changes 🤞 Partial fix without breaking changes for #1334 ## 📢 Changelog <!-- High level overview of important changes --> <!-- For example: fixed status bar manipulation; added new types declarations; --> <!-- If your changes don't affect one of platform/language below - then remove this platform/language --> ### Android - cleanup `layoutListener`; - track `attachedContent`; - cleanup `attachedContent`; - remove listener that has been attached to `attachedContent`; ## 🤔 How Has This Been Tested? Tested on Pixel 7 PRO (API 36). ## 📝 Checklist - [x] CI successfully passed - [x] I added new mocks and corresponding unit-tests if library API was changed
1 parent 83e34b7 commit 8393ee2

1 file changed

Lines changed: 5 additions & 1 deletion

File tree

android/src/main/java/com/reactnativekeyboardcontroller/listeners/WindowDimensionListener.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@ class WindowDimensionListener(
1818
) {
1919
private var lastDispatchedDimensions = Dimensions(0.0, 0.0)
2020
private var layoutListener: ViewTreeObserver.OnGlobalLayoutListener? = null
21+
private var attachedContent: ViewGroup? = null
2122

2223
public fun attachListener() {
2324
// attach to content view only once per app instance
2425
if (context != null && listenerID != context.hashCode()) {
2526
listenerID = context.hashCode()
2627

2728
val content = context.content
29+
attachedContent = content
2830

2931
updateWindowDimensions(content)
3032

@@ -38,7 +40,9 @@ class WindowDimensionListener(
3840
}
3941

4042
public fun detachListener() {
41-
context?.content?.viewTreeObserver?.removeOnGlobalLayoutListener(layoutListener)
43+
attachedContent?.viewTreeObserver?.removeOnGlobalLayoutListener(layoutListener)
44+
attachedContent = null
45+
layoutListener = null
4246
}
4347

4448
private fun updateWindowDimensions(content: ViewGroup?) {

0 commit comments

Comments
 (0)