(Android Studio) Simple implementation: Double-click the Back button to exit the application home page

Source: Internet
Author: User

Design Requirements: When a user wants to exit a page (often the first page), jump out of the toast prompt and press the back button to exit! ”。

When the user double-clicks for two times, the interval is less than 800 milliseconds, exit the app. Otherwise, only the prompt message is displayed, no action.

@Override public    void onbackpressed () {        if (System.currenttimemillis ()-mexittime <) {            ScrollingActivity.this.finish ();   Close this activity page        }        else{            toast ("then press the back key to exit!) ");            Mexittime = System.currenttimemillis ();   Here is the key to the assignment, don't forget        }    }

Analytical:

Ⅰ. Before the method, under the main class, declare the Mexittime variable and initialize it.

Private Long Mexittime = System.currenttimemillis ();  For the current system time, units: milliseconds

Ⅱ. Two clicks set at 800 milliseconds is experimentally validated to meet the human response rate.

The Ⅲ.toast () function is custom and easy to call.

Private void Toast (String content) {        toast.maketext (Getapplicationcontext (), Content,toast.length_short). Show ();    

By this point, the design is complete.

(Android Studio) Simple implementation: Double-click the Back button to exit the application home page

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.