Is this an Android context leak?

Here is a snippet of simplified code:

static Activity longLivedField;

onCreate(...) {     
    longLivedField = this;  // the only write to this field
}

I saw people claim this as a leak of context and create fixes for it. A typical solution is to invalidate the field at appropriate places. For example, in onPause():

onPause() {
    longLivedField = null;
}
+5
source share
1 answer

Yes, it is a memory leak if you do not reset the field in onPause (). You will almost certainly never want to keep a static link to any activity. What are you trying to achieve?

The Android developer website contains a handy page that describes how to avoid memory leaks like this:

Avoid memory leaks

+3
source

All Articles