Last Updated: February 25, 2016
·
1.885K
· themvrck

Leaking context in Android

In Java, non-static inner and anonymous classes hold an implicit reference to their outer class. Static inner classes, on the other hand, do not. Common examples of non-static inner and anonymous classes are Handler and Runnable.

When the life-cycle of your instance is more than that of Activity, due to the implicit reference, the Activity won't get garbage collected causing a memory leak.

To fix this, make your instance static and hold a weak reference to the enclosing Activity if you want to call its methods. The same holds true for Fragments.

1 Response
Add your response