Troubleshoot multiple clicks on Android to start several identical interfaces

Source: Internet
Author: User

In the Android development process we often encounter such a problem, when the user clicks on a view to launch a new activity, if quickly multiple clicks will launch multiple identical interface. While it's very rare for users to play their own mobile phones, it's really a bad experience when it happens. Just before a period of time to study Lanchmode, then we have to start the acitivity to Singletop, so that the top can only have a same interface, the problem should be solved it? Experiment, can only say that the basic satisfaction requirements, but there are some special circumstances such as the use of Startactivityforresult start time is not good use. Let's solve this problem once and for all.

Home, in the project we want to create a parent class activity, called Fatheractivity Bar, practice has proved that the use of such a parent in the project is a lot of benefits ah, not listed here. Then you need to rewrite the Dispatchtouchevent method of the parent class, just a few lines of code. The idea is that every time there is a pressing event to do the interval of judgment, if it is a quick click on the direct interception down.

Click ( here) to collapse or open

    1. @Override
    2. public boolean dispatchtouchevent (Motionevent ev) {
    3. if (ev.getaction () = = Motionevent.action_down) {
    4. if (Util.isfastdoubleclick ()) {
    5. return true;
    6. }
    7. }
    8. return super.dispatchtouchevent (EV);
    9. }

For quick Click Judgment, we can define the desired event interval by ourselves, where I define a second:

Click ( here) to collapse or open

    1. public static Boolean Isfastdoubleclick () {
    2. Long time = System.currenttimemillis ();
    3. Long TimeD = Time-lastclicktime;
    4. Lastclicktime = time;
    5. return truetimed <= 1000;
    6. }

Finally, just let all the activity be inherited fatheractivity.

Troubleshoot multiple clicks on Android to start several identical interfaces

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.