Search code examples
androidandroid-activity

Activity.onDestroy behind the scenes And what does destroy really mean?


It's familiar to me, that pressing back key will cause activity to get "destroyed", or when the developer calls function finish(), or when the system needs memory and etc...

And also familiar that we need to perform cleanup procedure in onDestroy like unBindDrawables ( see example ) for avoiding OutOfMemory Exceptions.

My question is:

does activity's destroy mean that reference to activity object is removed? i.e. activity object gets available to GC? If so why do we need to explicitly remove reference to activity's associated views?

Consider example:

If object A has a reference to object B and B is referred only by A, then if we nullify the A's reference there's no need to explicitly set B's reference to null, Both will GC-ed...

My intuition tells me that activity's case is something like this... Please tell me where is my mistake.

Thanks!


Solution

  • does activity's destroy mean that reference to activity object is removed? i.e. activity object gets available to GC?

    Yes, insofar as Android lets go of the activity. If you have a reference to it (directly or indirectly) from a static context, it will not be garbage-collected.

    If so why do we need to explicitly remove reference to activity's associated views?

    You do not "need to explicitly remove reference to activity's associated views".