How to stop the thread in android activity [duplicate]

In onStop() method, call interrupt as follows:

yourThreadObject.interrupt()

and in the run() method of the thread, have a condition that checks for Thread’s interrupt status. Depending on your implementation, you might want to enclose this check within a while loop as,

public void run() {
    while (!Thread.interrupted()) {
        //do something
        ...
    }
}

or you might check at certain points in the run() method as,

public void run() {
    //do something
    ...

    if (Thread.interrupted()) {
        return;
    }

    //do something
    ...
}    

Leave a Comment