On Wed, Apr 27, 2011 at 3:38 PM, Eric <e...@alum.mit.edu> wrote: > http://developer.android.com/guide/topics/fundamentals/activities.html > > Why does the doc say this, "For example, if your activity has a thread > running in the background to download data from the network, it might > create that thread in onCreate() and then stop the thread in > onDestroy()." > > - onDestroy() is not guaranteed to be called, so if you create a > Thread in onCreate(), the thread may never be stopped. > > And, why does the doc say this, " For example, you can register a > BroadcastReceiver in onStart() to monitor changes that impact your UI, > and unregister it in onStop() when the user can no longer see what you > are displaying. The system might call onStart() and onStop() multiple > times during the entire lifetime of the activity, as the activity > alternates between being visible and hidden to the user." > > - onStop() is not guaranteed to be called if the activity is killed by > Android, so your broadcast receiver would never be unregistered. > > Wouldn't the developer need to perform all of these functions in > onResume/onPause() instead?
If onStop() or onDestroy() are not called, that is because the process is being terminated. In that case, the thread and BroadcastReceiver go away automatically, along with everything else. That being said, it is often a good idea to clean these things up in onPause() (or onStop(), since onStop() is very very very very very likely to be called), if they consume RAM/CPU/battery, since you do not know how long you will be in the paused/stopped state. -- Mark Murphy (a Commons Guy) http://commonsware.com | http://github.com/commonsguy http://commonsware.com/blog | http://twitter.com/commonsguy _Android Programming Tutorials_ Version 3.3 Available! -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en