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