Skip to content

Instantly share code, notes, and snippets.

@davinctor
Created September 16, 2016 11:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davinctor/fab78bcef29afde1dd93d3dd4fbbafc3 to your computer and use it in GitHub Desktop.
Save davinctor/fab78bcef29afde1dd93d3dd4fbbafc3 to your computer and use it in GitHub Desktop.
Simple util for checking if app should be closed on back pressed.
/**
* 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