Android

뒤로가기 버튼 더블클릭

Superkill 2017. 12. 17. 09:54
반응형

BackPressCloseHandler.java

public class BackPressCloseHandler {

private long backKeyPressedTime = 0;
private Toast toast;
private Activity activity;
public BackPressCloseHandler(Activity context) {
this.activity = context;
}


public void onBackPressed() {
if (System.currentTimeMillis() > backKeyPressedTime + 1000) {
backKeyPressedTime = System.currentTimeMillis();
showGuide();
return;
}

if (System.currentTimeMillis() <= backKeyPressedTime + 1000) {
activity.finish();
toast.cancel();
}
}

private void showGuide() {
toast = Toast.makeText(activity, "\'뒤로\'버튼을 한번 더 누르시면 종료됩니다.",
Toast.LENGTH_SHORT);
toast.show();
}
}

MainActivity.kt


override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
...(생략)
backPressCloseHandler = BackPressCloseHandler(this)
}

override fun onBackPressed() {
backPressCloseHandler?.onBackPressed()
}


'Android' 카테고리의 다른 글

Unable to locate adb  (0) 2018.01.02
com.android.support:support-annotations 에러  (0) 2018.01.02
AndroidManifest.xml  (0) 2017.12.24
session app error installing apk  (0) 2017.12.13