I am new to android.At the time of closing application i need to stop the thread in android. Can anyone help me to solve this?
A:
You could call the finish() Activity method as such:
@Override
protected void onStop() {
finish();
super.onStop();
}
So, this way when you hit the home button or back out of the application completely, it should end it.
xil3
2010-07-04 13:21:41
That does not stop any threads you created yourself.
CommonsWare
2010-07-04 13:29:48
I know that, but from what I understood, he just wanted the application to close when hitting the home/back button rather than just sitting in the background.If I misunderstood, sorry.
xil3
2010-07-04 13:37:17
I just receive udp data for that i use thread.When i hit back button my application visual is closed but it receive the data after hit the back button.For that i ask this.Thanks for the reply
Sumi
2010-07-04 16:50:06
+2
A:
The best solution is to not create the thread yourself in the first place. If the background thread is just to do a bit of work, consider using AsyncTask
instead of your own thread. Or, if this is a service, consider using IntentService
.
Otherwise, I hope your background thread is blocking on something (e.g., waiting on a socket, waiting on a LinkedBlockingQueue
). In that case, you can terminate the thread by doing something to what it is blocking upon (e.g., closing the socket, sending a message on the LinkedBlockingQueue
to tell the thread to fall out of its work loop).
CommonsWare
2010-07-04 13:32:19
You're assuming it's something a lot more complicated than it is.How do you know he's creating a thread to begin with?
xil3
2010-07-04 13:38:31
@xil3: The question is poorly written. We could both be wrong, and the OP is referring to a thread that is pulling out of his shirt. I am taking the guess that since the OP inquired about a thread, the OP created one -- otherwise, I don't know why the OP would think he needs to stop some thread.
CommonsWare
2010-07-04 14:11:58