tags:

views:

51

answers:

2

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
That does not stop any threads you created yourself.
CommonsWare
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
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
+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
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
@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