android tutorial - Anonymous callback in activities in android | Developer android - android app development - android studio - android app developement
Anonymous callback in activities in android
Every Time you create an anonymous class, it retains an implicit reference to its parent class. So when you write:
When the user navigates away from your LeakyActivity, this reference can prevent the LeakyActivity instance from being garbage collected. This is a serious leak as activities hold a reference to their entire view hierarchy and are therefore rather large objects in memory.
How to avoid this leak: You can of course avoid using anonymous callbacks in activities entirely. You can also unregister all of your callbacks with respect to the activity lifecycle. like so: