I have a ListView
and in it's adapter
's getView
method, I return a RelativeLayout
with MyButton
inside it.
MyButton
has a textView
and I have clickable words inside it (ClickableSpan
).
To make this work, I start with thew following line:
textView.setMovementMethod(LinkMovementMethod.getInstance());
Everything works perfectly but MAT shows that MyButton
leaks because of textView
. When I comment out the line above, nothing leaks.
Shall I set movementMethod
to null
? But even if so, I can't know the destruction moment of the button to set that to null
as it is inside of many other views.
What am I doing wrong? How to prevent this leak?
update
Solved the leak by setting text to empty string inside onDetachedFromWindow
, but I am still trying to find a documentation related to this behaviour. Why should I set the textview
to ""
?
movementMethod
tonull
did not work but setting text to""
did work. (in onDetachedFromWindow). If you also know the reason of this leak, please post as an answer so I can mark it as accepted answer. I am still curious why this leak happens. There are no documentation related to this behaviour. – NelleTextView.setMovementMethod()
was the culprit. Unfortunately for me, setting the movement method to null and the text to "" inonDetachedFromWindow()
hasn't solved the problem. LeakCanary shows that it has something to do withViewTreeObserver
not having a preDraw listener cleared. Given thatTextView
implementsOnPreDrawListener
, I wonder if that's doing something funky under the hood? – Tanika