onPause() is not an option since it is called even more frequently than
onDestroy(). Perhaps the most reasonable way is to indeed hook on
onDestroy() but to kill the background thread only if
onRetainNonConfigurationInstance() hasn't been called before onDestroy().

2011/2/10 Roger Podacter <[email protected]>

> Have you tried using onPause rather than onDestroy? Not sure that
> helps this situation. But destroy does not get called too often. But
> on pause is guaranteed to get called every time the user presses the
> home button, or back, etc.
>
> On Feb 9, 12:36 pm, Kaloian Doganov <[email protected]> wrote:
> > The onDestroy() is called on various occasions, not necessary related
> > to user "exiting" the application. If you effectively stop your
> > background thread in onDestroy(), your background task would not
> > survive a simple orientation change.
> >
> > On Feb 2, 1:07 pm, Serdel <[email protected]> wrote:
> >
> >
> >
> > > In my application I am connecting to a server and downloading some
> > > things. Not much but the connection it self takes a while. Off course
> > > this is done in a thread (class that implements runnable). If some
> > > error occurs, the thread sends a message to the UI using a Handler and
> > > the UI displays an dialog window.
> >
> > > My problem is that I can't stop the thread in the UI properly. For
> > > example if during the connection I would press the 'home' button on
> > > the phone, after some seconds I get an error saying that my
> > > application couldn't create a dialog window (because it is not running
> > > anymore). That means that my thread was not stopped and kept on
> > > working until an error (i.e. timeout) occurred and tried to send a
> > > massege to the UI by a handler.
> >
> > > to Stop the thread I have a function that I found:
> >
> > >                   public synchronized Thread stopThread(Thread thr){
> > >                           if(thr != null){
> > >                             Thread temp = thr;
> > >                           thr = null;
> > >                             temp.interrupt();
> > >                           }
> > >                           return thr;
> > >                         }
> >
> > > And I use it like:
> >
> > > @Override
> > >         public void onDestroy()
> > >             {
> > >                 super.onDestroy();
> > >                senderTh= stopThread(senderTh);
> > >                finish();
> > >             }
> >
> > > Why this doesn't stop my Thread? How can I do it from the UI?
>
> --
> 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
>

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

Reply via email to