Based on Android developer's document web-page AsyncTask, Thread, it is possible to send cancel()
request to AsyncTask
's thread. Albeit I have sent the cancellation request and it was successful, the thread stills running. Any idea to stop the execution?
button.setOnClickListener {
Log.i(TAG, asyncTaskObj.status) // RUNNING
if (asyncTaskObj.status == AsyncTask.Status.RUNNING) {
asyncTaskObj.cancel(true)
Log.i(TAG, asyncTaskObj.isCancelled) // True
}
Log.i(TAG, asyncTaskObj.status) // RUNNING
}
Plus, is there any simple alternative to AsyncTask
, which let's restart execution of the thread? (Obviously restarting AsyncTask
is not possible Q&A-2)
One more thing, I have read following questions Q&A-1, Q&A-2. However, the answers do not work for me. For instance, there is no isRunning
variable.
P.S. My code is in Kotlin. Please share your idea in either Java or Kotlin.