Sumi
Sumi

Reputation: 71

Stop thread at the time of closing application in android?

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?

Upvotes: 1

Views: 2990

Answers (2)

CommonsWare
CommonsWare

Reputation: 1006604

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

Upvotes: 2

xil3
xil3

Reputation: 16439

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.

Upvotes: 2

Related Questions