Original URL: http://blog.sina.com.cn/s/blog_6714fba70100wtx1.html
Handle's Removecallbacks function is used in Android development, which functions as:
But sometimes the actual work will appear removecallbacks invalid, solve it.
Figure: Two buttons, one adds the runnable to the message queue, and one removes the runnable from the message queue. The runnable prints logs every 3 seconds.
Code:
Results:
(1) start–> output –> end–> stop output
(2) start–> output –> background–> front–> continue output
Removecallbacks cannot remove Updatethread from the message queue when the activity is running in the background and then into the foreground.
What is this for?
During activity from the foreground to the background, the thread is always running, but when the activity is transferred to the foreground, the runnable updatethread is redefined, i.e. from the message The updatethread of the queue removal is not the same object as the Updatethread that was originally added to the message queue. If the updatethread is defined as static then Removecallbacks will not fail.
At this point, Post,postdelayed,removecallback is facing the same handler,updatethread, so it can implement remove.
This reason, even if the correct is the surface, the specific reasons need to go deep into the underlying mechanism to understand, the solution ...
"Turn" removecallbacks failure reason in Android