+ 3

Please explain this code.

private static final int TIME_INTERVAL = 2000; private long mBackPressed; @Override public void onBackPressed() { if (mBackPressed + TIME_INTERVAL > System.currentTimeMillis()) { super.onBackPressed(); return; } else { Toast.makeText(getBaseContext(), "Tap back button in order to exit", Toast.LENGTH_SHORT).show(); } mBackPressed = System.currentTimeMillis(); }

21st Jun 2018, 5:59 PM
Prince Raj
2 Respostas
+ 13
It's a "press back twice to exit" code. By the time you press back once, the current system time in milliseconds is stored in the mBackPressed variable. Every time you click the back button, the difference of "currentTime - lastClickTime" is calculated based on that variable and if its less than 2000 millis (2 secs) its considered as a consequtive doubleclick, if the user re-taps the back button after more than 2 seconds, its not considered as a doubletap...
21st Jun 2018, 6:21 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 3
if you don't know it, a toast is a rounded rectangle at the two sides which displays a piece of text it has two display times, long or short just a back button snippet
21st Jun 2018, 6:27 PM
Abdur-Rahmaan Janhangeer
Abdur-Rahmaan Janhangeer - avatar