2

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!

tsobe
  • 179
  • 4
  • 14

2 Answers2

2

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".

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thanks in advance CommonsWare, I'm satisfied about first question. – tsobe Aug 09 '12 at 21:05
  • But about second... Why then "unbindDrawables" is work-around to OutOfMemory errors? Or maybe I misunderstood the meaning of unbindDrawables? – tsobe Aug 09 '12 at 21:11
  • @dioholic: Nothing in that blog post is absolutely necessary in general, though parts of it (particularly the background stuff) may be useful in certain scenarios. See http://stackoverflow.com/a/7045044/115145. – CommonsWare Aug 09 '12 at 22:46
0

When you press back button your app isn't destroyed but paused and the SO calls the onPause() method. It can be destroyed if the SO detects, for example, a low memory condition then it destroy and will call onDestroy method. In this method you have to clean up threads (if you started them) or everything that is unnecessary anymore and cause the resource consuption. If you are familiar with Servlet the onDestroy method in android is very similar. Hope this help you

FrancescoAzzola
  • 2,666
  • 2
  • 15
  • 22