I created a thread in my activity, and that thread will print out the value of my instance variables in my activity after running for about 10 seconds. I pressed the back key to destroy my activity before the thread prints out the values, and when the thread reaches that line of code, it can still print out the values correctly. That means even though the activity is finished, the java object of the activity is still there. Will the object always be there? Or it is just waiting to be GCed? Or it will be killed only when Android needs memory?
Asked
Active
Viewed 823 times
1 Answers
2
See this question: your Thread will not be garbage collected even if the activity is destroyed. You need to specifically request your Thread to terminate in your onFinish() function. Then either let it kill itself or do a join() in order to wait for it to actually terminate.

Community
- 1
- 1

Vincent Mimoun-Prat
- 28,208
- 16
- 81
- 124
-
So, because the thread can't be GCed, and the activity still holds a reference to the thread, the activity will not be GCed until the thread finishes? – user412759 Nov 06 '10 at 02:57
-
You can set the thread reference to null in your activty#onDestroy method. This way the activity does not hold a reference to the thread anymore. However the thread will continue to run after the activity gets GCed. – Vincent Mimoun-Prat Nov 11 '10 at 16:40