Thanks Dianne. Ok, I could implement something like this :
- implement a counter inits to 0 when the thread is started - when an Activity starts (in method onStart() ), increment the counter - when an Activity stops (in method onStop() ), decrease the counter - when the counter equals 0, stop the thread However, I think a problem can occurs when the system need resource and kills the activity in the onPause() method. So the onStop() method of the activity is not called => the counter is not decreased => the Thread will not be stopped ! Q4 : Is it possible to handle this situation, when system kills the activity ? Q5 : Can you confirm me that onStop() and onDestroy() activity's methods are not called when the system kills the activity in the onPause() method ? On 18 nov, 19:02, Dianne Hackborn <hack...@android.com> wrote: > You don't "exit" an application; in fact an application is a fairly nebulous > concept on Android. You should do this kind of stuff in Activity -- for > example, as long as you have an activity in the start state, then continue > running your work thread. > > > > On Wed, Nov 18, 2009 at 2:02 AM, Syl <sylvain.guyoma...@altran.com> wrote: > > I would like to know how to execute code before exiting application. > > > My application is composed of several activities. When the user starts > > the application (from the launcher menu or from a notification in the > > status bar), he goes in an InitActivity where I process some > > initializations. From user experience, application always restarts. > > > One of these initializations is to init a network manager and start a > > thread that periodically sends a network request to notify a server > > that application is active. > > > I need to process some end operations, like stopping this thread when > > user quits application, for example by pressing the HOME key or BACK > > key when he's in the root activity. > > > However, when the user press the HOME key, application is still > > running in background. > > > Here are my questions : > > > Q1 : is it possible to execute code when exiting application (I see > > the onTerminate() method of the class Application but it seems not to > > be always called) ? > > > Q2 : Is it possible to force application exit (activity method finish > > () only destroy the current activity, not the application) ? > > > Q3 : Is it possible to handle HOME key press (onKeyDown() is not > > called when this button is pushed) ? > > > Thanks for your help. > > > -- > > 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<android-developers%2bunsubscr...@googlegroups.com> > > For more options, visit this group at > >http://groups.google.com/group/android-developers?hl=en > > -- > Dianne Hackborn > Android framework engineer > hack...@android.com > > Note: please don't send private questions to me, as I don't have time to > provide private support, and so won't reply to such e-mails. 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 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