Skip to content

Commit a5ddd58

Browse files
committed
Fix memory leak in InputView/CandidatesView
- avoid creating multiple `eventHandlerJob`s - remove viewTreeObserver listener on view detach
1 parent 96506d1 commit a5ddd58

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

app/src/main/java/org/fcitx/fcitx5/android/input/CandidatesView.kt

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import android.content.res.ColorStateList
1010
import android.graphics.Color
1111
import android.view.View
1212
import android.view.ViewGroup
13+
import android.view.ViewTreeObserver.OnPreDrawListener
1314
import android.widget.ImageView
1415
import androidx.annotation.DrawableRes
1516
import androidx.annotation.Size
@@ -28,6 +29,7 @@ import org.fcitx.fcitx5.android.daemon.FcitxConnection
2829
import org.fcitx.fcitx5.android.data.theme.Theme
2930
import org.fcitx.fcitx5.android.input.candidates.CandidateItemUi
3031
import org.fcitx.fcitx5.android.input.preedit.PreeditUi
32+
import org.fcitx.fcitx5.android.utils.styledFloat
3133
import splitties.dimensions.dp
3234
import splitties.resources.drawable
3335
import splitties.views.backgroundColor
@@ -63,7 +65,9 @@ class CandidatesView(
6365
field = value
6466
visibility = View.GONE
6567
if (field) {
66-
setupFcitxEventHandler()
68+
if (eventHandlerJob == null) {
69+
setupFcitxEventHandler()
70+
}
6771
} else {
6872
eventHandlerJob?.cancel()
6973
eventHandlerJob = null
@@ -145,18 +149,20 @@ class CandidatesView(
145149
}
146150
}
147151

152+
private val updatePositionListener = OnPreDrawListener {
153+
if (shouldUpdatePosition) {
154+
updatePosition()
155+
}
156+
true
157+
}
158+
148159
private val recyclerView = recyclerView {
149160
isFocusable = false
150161
horizontalPadding = dp(8)
151162
adapter = candidatesAdapter
152163
layoutManager = candidatesLayoutManager
153164
// update position after layout child views and before drawing to avoid flicker
154-
viewTreeObserver.addOnPreDrawListener {
155-
if (shouldUpdatePosition) {
156-
updatePosition()
157-
}
158-
true
159-
}
165+
this@CandidatesView.viewTreeObserver.addOnPreDrawListener(updatePositionListener)
160166
}
161167

162168
private fun createIcon(@DrawableRes icon: Int) = imageView {
@@ -274,6 +280,7 @@ class CandidatesView(
274280

275281
override fun onDetachedFromWindow() {
276282
handleEvents = false
283+
viewTreeObserver.removeOnPreDrawListener(updatePositionListener)
277284
super.onDetachedFromWindow()
278285
}
279286
}

app/src/main/java/org/fcitx/fcitx5/android/input/InputView.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@ class InputView(
8686
set(value) {
8787
field = value
8888
if (field) {
89-
setupFcitxEventHandler()
89+
if (eventHandlerJob == null) {
90+
setupFcitxEventHandler()
91+
}
9092
} else {
9193
eventHandlerJob?.cancel()
9294
eventHandlerJob = null

0 commit comments

Comments
 (0)