kotterknife icon indicating copy to clipboard operation
kotterknife copied to clipboard

Added Butterknife.reset() for reseting cached views

Open pouljohn1 opened this issue 9 years ago • 6 comments

Updated @MichaelRocks approach for the newest kotterknife version #5

pouljohn1 avatar Feb 10 '16 09:02 pouljohn1

according to WeakHashMap javadoc:

The value objects in a WeakHashMap are held by ordinary strong references. Thus care should be taken to ensure that value objects do not strongly refer to their own keys, either directly or indirectly, since that will prevent the keys from being discarded. Note that a value object may refer indirectly to its key via the WeakHashMap itself; that is, a value object may strongly refer to some other key object whose associated value object, in turn, strongly refers to the key of the first value object. One way to deal with this is to wrap values themselves within WeakReferences before inserting, as in: m.put(key, new WeakReference(value)), and then unwrapping upon each get.

And in your implementation: WeakHashMap will hold a strong reference to MutableCollection, MutableCollection will hold a strong reference to Lazy, Lazy will hold a strong reference to some View in an activity. According to Android source code, View will hold a strong reference to Activity, this will prevent Activity from being garbage collected.

cmicat avatar May 10 '16 09:05 cmicat

@cmicat WeakHashMap will hold a strong reference to MutableCollection, which is another WeakHashMap. So no, nothing will prevent an Activity from being GCed. But the original patch is one and a half year old so who cares?

MichaelRocks avatar May 10 '16 09:05 MichaelRocks

OK, it's my fault, but somewhat I can't find getOrPut definition. Maybe kotlin stdlib's change?

cmicat avatar May 10 '16 12:05 cmicat

getOrPut existed in Kotlin for ages and still presents in stdlib.

MichaelRocks avatar May 10 '16 14:05 MichaelRocks

We need this functionality to correctly implement Fragments being detached and re-attached (ex: ViewPager).

tinsukE avatar Aug 08 '17 15:08 tinsukE

I have forked the project, merged this PR, updated everything to latest and published it on JitPack if anybody is interested: https://github.com/rubengees/kotterknife

rubengees avatar Aug 08 '17 15:08 rubengees