Created
September 16, 2016 11:58
-
-
Save davinctor/fab78bcef29afde1dd93d3dd4fbbafc3 to your computer and use it in GitHub Desktop.
Simple util for checking if app should be closed on back pressed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Simple util for checking if app should be closed on back pressed. | |
*/ | |
public class BackPressUtil { | |
private long timeout; | |
private long prevCheck = -1; | |
public BackPressUtil(long timeout) { | |
this.timeout = timeout; | |
} | |
public BackPressUtil(long timeSpan, @NonNull TimeUnit timeUnit) { | |
this(TimeUnit.MILLISECONDS.convert(timeSpan, timeUnit)); | |
} | |
/** | |
* Check if activity should be finished. | |
* @return true if activity should be finished, false otherwise | |
*/ | |
public boolean shouldBeFinished() { | |
long timestamp = System.currentTimeMillis(); | |
if (prevCheck < 0 || timestamp - prevCheck > timeout) { | |
prevCheck = timestamp; | |
return false; | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment