Aaargh! Thank you for this post, as it led me to fix a memory leak where my app would consume 50K more with every run. I was looking into all sorts of context leaks, but it was just that threads are not automatically killed when pressing the back button to "exit" the app, so if one launches a thread in onCreate(), one adds about 10K per run, and DDMS will show a growing pile of threads as the run count increases...
Thanks On Jan 24, 4:04 pm, Guillaume Perrot <[email protected]> wrote: > If you want to stop athreadthat is running long operations, you > should read this:http://g.oswego.edu/dl/cpj/cancel.html. > In your service onDestroy you should not "join" yourthread, just > launch the cancel operation, the trick with all lifecycle methods > (including services) is that they are called in the mainthread. > If you block on the mainthread, you won't be able to start a new > activity from this process (the process is rarely stopped, and > relaunched automatically when a crash occurs), it will freeze and > you'll see the "sorry dialog" with the "wait" and "force close" > buttons. > > On Jan 24, 11:01 am, Mariano Kamp <[email protected]> wrote: > > > And when implementing stuff as a service where would the actual long running > > functionality go? > > In a separatethread? > > How should I stop thatthread? > > > Is acting on Service.onDestroy() the right time and will it definitively be > > called? > > How much time do I have in onDestroy()? > > Is it acceptable behavior to set a flag that the backgroundthreadshould > > stop and then wait for thethreadsignaling that it is finished? > > > To illustrate that, think of a Download Service that should download four > > files. > > When onDestroy() is called a flag shouldShutdown is set to true and > > onDestroy() waits then for thethreadto finish (Thread.join()). > > Thethreaditself is in the midst of downloading the 2nd file and monitors > > shouldShutdown and doesn't start a new download (if possible doesn't even > > ask for the next bytes) when it is set, but cleans up resources (open > > connections) and terminates itself. > > > Is that ok? > > > It would certainly not help the OS to quickly get rid of the service, at > > least when the service is busy with a "long" lasting operation. > > > On Fri, Jan 23, 2009 at 6:45 PM, Dianne Hackborn <[email protected]>wrote: > > > > On Fri, Jan 23, 2009 at 7:47 AM, g1bb <[email protected]> wrote: > > > >> I realized this right after I posted, by running a bunch of other apps > > >> while my app was still running. It seems like 'setPersistent' on the > > >> activity should most likely prevent this. Any ideas? > > > > DO NOT DO THAT. > > > > Please read this:http://code.google.com/android/intro/lifecycle.html > > > > Basically you should have things you want to continue running in the > > > background implemented as a Service. > > > > Also there is generally no need to create a whole newThreadfor this kind > > > of stuff, you can just post delayed messages to your own handler. > > > > Finally, for something like a countdown timer, you really might want to > > > consider using the alarm manager so you don't need to keep your app > > > running > > > at all while it is in the background. That is the kind of thing a well > > > behaving Android app will do. To be able to show the remaining time if > > > the > > > user returns to your activity, you can store on SharedPreferences the time > > > the countdown was started. > > > > Using the alarm manager is also the only way you can make sure you execute > > > when the time expires, even if the user has turned off the phone. > > > >> Thanks again. > > > >> On Jan 23, 8:36 am, Torgny <[email protected]> wrote: > > >> > Your application background process might get killed if you run > > >> > another application that takes up a lot of memory, for instance the > > >> > browser with several windows. As far as I understand it, the > > >> > application in the foreground takes priority as far as memory and > > >> > processing power goes over services and threads. > > > >> > On Jan 23, 9:15 am, g1bb <[email protected]> wrote: > > > >> > > Hello, > > > >> > > I've created an app that functions as a countdown timer via athread, > > >> > > a wakelock, and a handler back to my activity. My question is, what > > >> > > will cause mythreadto die, or app to not be running in memory > > >> > > anymore? I've had this happen once, and can't seem to recreate it > > >> > > again. > > > >> > > Thanks in advance!- Hide quoted text - > > > >> > - Show quoted text - > > > > -- > > > Dianne Hackborn > > > Android framework engineer > > > [email protected] > > > > Note: please don't send private questions to me, as I don't have time to > > > provide private support. All such questions should be posted on public > > > forums, where I and others can see and answer them. > > --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---

