Open source China oschina Android Client Source code Analysis (10) Double Exit program

Source: Internet
Author: User

In the source code, the fire ant completely encapsulates the function of the double-exit program: Doubleclickexithelper class

This type of source analysis is as follows:

1 in the constructor, the device context is passed in, the interface of exiting function is implemented, and the initialization of the message processor is completed. If you want a cue bar, you also need a toast object.

2 since it is fully encapsulated, then we need to pass the key event and key code in, because the interface rewrite onkeydown event needs to return a Boolean value, then in the Doubleclickexithelper class also customizes a return boolean value of the  The OnKeyDown function.

3onKeyDown function:

3.1 If the key is not pressed, return false;isonkeybacking as a global variable, initialize doubleclickexithelper object, the default value is False, When the first click is False, the isonkeybacking is set to true to finish the toast initialization, Execute a business with Handler.post (runnable) delay-If the toast is still displayed, it is manually shut down directly. When the second click isonkeybacking is already true, this time there is a possibility: runnable delay execution of the business has not been executed (from the source we can see, this time is 2 seconds, that is, 2 seconds to click the return button again), we can first remove the delay to perform the task, Then directly close the toast, perform the operation of the close interface, and so on. Returns true after execution, representing a response to the keydown event.

3.2 The advantages of this method in addition to encapsulation, there is a manual closure of the toast, generally in use, the display time is not short is a long, but after the interface is closed, when the toast display after the shutdown when the device context is not found, there is a program exited, but the toast has always shown that I have encountered this problem.

Read the code, is a good habit, you insist!


Open source China oschina Android Client Source code Analysis (10) Double Exit program

Related Article

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.