Created
May 6, 2017 12:08
-
-
Save YusukeIwaki/017e492cfae432719d2a722c34c1a7f9 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
package io.github.yusukeiwaki.example; | |
import android.os.Handler; | |
import android.os.Message; | |
// Activityに以下のような感じで実装する | |
// | |
// @Override public void onBackPressed() { | |
// if (backPressHandler.handleBackPressed()) { | |
// Toast.makeText(this, "アプリを終了するにはもう一回戻るキーを押して", Toast.LENGTH_SHORT).show(); | |
// } else { | |
// super.onBackPressed(); | |
// } | |
// } | |
// | |
public class BackPressHandler extends Handler { | |
private boolean enabled; | |
public boolean handleBackPressed() { | |
if (enabled) return false; | |
enabled = true; | |
sendEmptyMessageDelayed(1, 2500); | |
return true; | |
} | |
@Override public void handleMessage(Message msg) { | |
if (msg.what == 1) { | |
enabled = false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment