Reputation: 5576
public class TestActivity extends Activity {
public static TestActivity mTestActivity;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mTestActivity = this;
}
@Override
protected void onDestroy() {
mTestActivity = null;
super.onDestroy();
}
}
Can I ask a very rookie question?
Does static link to Activity ALWAYS leads to memory leak? Even if I 'null' it on destroy?
EDIT:
If there is a memory leak even though you 'null' it on destroy, could you explain why?
I have some years of Java experience but I cannot understand why 'null' on destroy does not free the memory.
Upvotes: 0
Views: 221
Reputation: 34016
onDestroy
runs once - if ever. Till then you have your leak. Unregister in onPause()
. Notice that pre-Honeycomb onPause()
might be the last method that would run - not that it makes a difference in your case as a process being killed takes its classes with it (and their leaks). Or so I think. I don't know if an activity can be killed without its onDestroy being called (not in the case the whole process goes down - then it is perfectly possible but it also makes no difference as I said) - I don't think so though.
In short your leak exists as long as your activity is alive (instantiated)
Upvotes: 0
Reputation: 11439
If, you null it on destroy there is not point in keeping it static. As for the leak, yes, I think you will (if you change activities). It would be easier to just keep a (non-static) reference to the application.
Upvotes: 1