asynctask

Interrupting/cancelling threads

SDK Version: 
M3

threadWhen you run a background operation using a Thread, or AsyncTask in most of the times it is needed to be able to interrupt it.
In lot of cases, when the user starts a sceen in our application, a thread is started in the background, to load its content. Hoever if the user leaves the screen before its loading is complete, the loading process should be interrupted. Even if the loading prosess can not be interrupted immediately, or it would be too mutch troube, it shold detect that the display of the result is no longer needed.

So how do you stop a thread exactly? You can see in the documentation that Thread class has a stop() method. This method offered an "easy" way to interrupt threads in older android versions, by killing the thread. It is deprecated now, do not use it, it may left things in inconstent state, among other problems, so this was an unelegant way.

Syndicate content