"Turn" removecallbacks failure reason in Android

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.