I am a beginner to Android but I seem to be running across a problem where my Activities are never being garbage collected because there is a still a reference to the Activity in the non-static inner class of my thread the variable is called this$0 I found it by using the MAT in Eclipse. Below is my current setup.
public class MyActivity
-->class MySurfaceView
---->class MyThread
When trying to find information on if it was possible to remove this last instance it appears I can’t after finding a very useful article here.
Avoid non-static inner classes in an activity if you don’t control
their life cycle, use a static inner class and make a weak reference
to the activity inside. The solution to this issue is to use a static
inner class with a WeakReference to the outer class, as done in
ViewRoot and its W inner class for instance
So it seems like I shouldn’t use this system of inner classes or use WeakReferences.
My question is, why do the sample projects use the inner class system with no WeakReferences? Are they still garbarge collected? Or were they designed not to deal with garbage collection?
Is it likely my application has a code problem, meaning I should correct my code and still keep my inner classes? Or should I seperate the classes (or introduce WeakReferences)?
You can use inner classes make them static and have a Weak Reference the parent.
Also store long living objects in Application Context.