Yes, a Service is what you want to use if you want to continue doing work
after the user has exiting your app.

The original poster I think is dealing with thread that are just supposed to
be running while the app is being used, in which case I would avoid using a
Service, since that is likely to introduce other more subtle problems where
your app continues needing to be running even when the user has left it,
consuming memory and cpu.

I think the best way to deal with this is to introduce a static Handler
subclass for receiving messages from the threads, which has a pointer to the
current activity.  Return that object in onRetainNonConfigurationInstance()
and set its activity pointer to 0; when the next activity enters onCreate(),
retrieve the object with getLastNonConfigurationInstance() and point it to
the new activity.  You are guaranteed no messages will be dispatched on the
main thread between onRetainNonConfigurationInstance() and the next
activity's onCreate() call.

On Wed, Mar 11, 2009 at 7:33 AM, Streets Of Boston
<flyingdutc...@gmail.com>wrote:

>
> I've been dealing with the same issues and i tried all these. And
> nothing seems to work well.
> First of all, having a thread that's part of your activity running
> after your activity's onDestroy has been called is tricky. It can lead
> to memory-leaks, etc.
>
> I finally decided to create a Service for these kinds of tasks and it
> simplified everything. It's an initial expense writing a Service, but
> it pays off in your activity's code and its life-cycle handling.
>
> Your activity binds to the service on the onCreate and unbinds it on
> the onDestroy (or, even better, on onResume and onPause).
> To prevent the Service from shutting down, call startService from your
> activity as well, just after binding to it. When your activity
> unbinds, the service won't be shut down and your service's threads can
> keep running safely. When your activity is recreated, just re-bind to
> the service, have your activity query the service for the most current
> state of your Service's threads and voila!
>
> Your Activity:
>  Register a callback service. The service can call your activity back
> with important updates:
>    Handle callbacks from service.
>
>  onCreate:
>     bind to service.
>     start-service as well. This will keep the service alive when
> unbinding from it.
>     request callback from service giving the latest state/info of the
> service's threads/process.
>
>  onDestroy:
>     unbind from service.
>
> On Mar 11, 10:18 am, Stoyan Damov <stoyan.da...@gmail.com> wrote:
> > unless he traps the back button in onKeyDown
> >
> >
> >
> > On Wed, Mar 11, 2009 at 2:51 PM, Marco Nelissen <marc...@android.com>
> wrote:
> >
> > > That will solve the problem when opening/closing the keyboard, but
> > > won't solve it when e.g. starting the app and then immediately hitting
> > > the back button to exit it.
> >
> > > On Wed, Mar 11, 2009 at 5:37 AM, Stoyan Damov <stoyan.da...@gmail.com>
> wrote:
> >
> > >> keyboardHidden|orientation
> >
> > >> On Wed, Mar 11, 2009 at 2:34 PM, mobilekid <mobilek...@googlemail.com>
> wrote:
> >
> > >>> For some reason that seems not to work in my case. I've declared the
> > >>> activity as android:configChanges="orientation" in the
> > >>> AndroidManifest.xml, which I assume will call onConfigurationChanged
> > >>> (Configuration). So for testing purposes I've simply implemented it
> as
> > >>> follows:
> >
> > >>> @Override
> > >>> public void onConfigurationChanged (Configuration newConfig){
> > >>>        Log.i(this.toString(), "I've been called!");
> > >>>        super.onConfigurationChanged(newConfig);
> > >>> }
> >
> > >>> However, it never gets called. Am I doing anything wrong here?!
> > >>> Thanks!
> >
> > >>> On Mar 11, 11:44 am, Stoyan Damov <stoyan.da...@gmail.com> wrote:
> > >>>> Yes, basically if you declare your activity's orientation to be
> > >>>> "sensor" there's nothing else to do (I think).- Hide quoted text -
> >
> > - Show quoted text -
> >
>


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

Reply via email to