[android-developers] Stopping thread when last activity finishes

2010-03-02 Thread michael
Hi all,

I am struggling to find a good way of achieving a very simple
objective, namely to maintain a thread that lives from the first
activity of my app is started until the last activity is stopped.

I can create a thread whenever I like, but even if I mark the thread
as daemon, it continues to run after all my activities have stopped.
The reference to the thread is held by an Application subclass. Would
be great if I could rely on the Application.onTerminate() method, but
unfortunately this doesn't necessarily get invoked when the app
terminates.

Surely there must be a simple solution?

Any suggestions would be greatly appreciated.
Best,
Michael.




-- 
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


Re: [android-developers] Stopping thread when last activity finishes

2010-03-02 Thread Mark Murphy
michael wrote:
 Hi all,
 
 I am struggling to find a good way of achieving a very simple
 objective, namely to maintain a thread that lives from the first
 activity of my app is started until the last activity is stopped.
 
 I can create a thread whenever I like, but even if I mark the thread
 as daemon, it continues to run after all my activities have stopped.
 The reference to the thread is held by an Application subclass. Would
 be great if I could rely on the Application.onTerminate() method, but
 unfortunately this doesn't necessarily get invoked when the app
 terminates.
 
 Surely there must be a simple solution?

Step #1: Create a service.

Step #2: Move your thread logic to the service.

Step #3: Have each activity that cares about the thread's existence bind
and unbind to and from the service.

Step #4: Have the thread stop in onDestroy() of the service.

-- 
Mark Murphy (a Commons Guy)
http://commonsware.com | http://twitter.com/commonsguy

Android App Developer Books: http://commonsware.com/books

-- 
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