If the task is actually cancellable -- that is, if it actually stops when you cancel it -- that should be sufficient. If you implement the AT's onCancelled method, make sure it doesn't use any stale references.
G. Blake Meike Marakana Programming Android 2ed is now in stores: http://bit.ly/programmingandroid On Wednesday, January 16, 2013 4:35:53 AM UTC-8, Greenhand wrote: > > In my scenario, I have a task to run but I do not want it to block my UI > thread. Therefore, I adopt AsyncTask. IntentService can run a task at a > time on another Thread but I do not know how to cancel it as mentioned > earlier. > > The problem I would like to deal with is that when user press back to > destroy the activity, I would like to abort the task and make sure there is > no memory leak. It is why I would like to cancel the task. > I expect a safe way to abort my task and destory the activity without > memory leak. If it can be done, there is no problem that there is nobody to > report and the task is running. None of them should be alive when user > press back. To let AsyncTask to reference an Activity safely, it seems > performing clean-up in onPause() is the last chance to be safe. However, it > is a little early in life cycles. Sometimes, the task should be abort only > if the activity is destoryed. > > I have not tried the FragmentRetainInstance.java in API demos but It > seems Fragment is the solution to my problem. Am I right? > > G. Blake Meike於 2013年1月16日星期三UTC+8上午1時44分04秒寫道: > >> ... >> If the AsyncTask holds a reference to an Activity, you'll leak the >> activity. That's true even if the reference is implicit (that is, if the >> AsyncTask is an inner class and *not* static). Making sure that the task >> has no pointers to the Activity, after onPause, is safe. On the other >> hand, it brings up the question of why the Task is still running, if there >> is nobody to whom to report a result... >> >> G. Blake Meike >> Marakana >> >> Programming Android 2ed is now in stores: >> http://bit.ly/programmingandroid >> >> -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en

