public class MainActivity extends AppCompatActivity {
final Context context = this;
private BackKeyClickHandler backKeyClickHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
backKeyClickHandler = new BackKeyClickHandler(this);
}
@Override
public void onBackPressed() {
backKeyClickHandler.onBackPressed();
}
public class BackKeyClickHandler {
private long backKeyClickTime = 0;
private Activity activity;
public BackKeyClickHandler(Activity activity) { this.activity = activity; }
public void onBackPressed() {
if (System.currentTimeMillis() > backKeyClickTime + 2000) {
backKeyClickTime = System.currentTimeMillis();
showToast(); return;
}
if (System.currentTimeMillis() <= backKeyClickTime + 2000) activity.finish();
}
public void showToast() {
Toast.makeText(activity, "뒤로 가기 버튼을 한 번 더 누르면 종료됩니다.", Toast.LENGTH_SHORT).show();
}
}
}
728x90
반응형
'Frontend > Android' 카테고리의 다른 글
안드로이드) 리싸이클러뷰(RecyclerView) (0) | 2022.01.05 |
---|---|
안드로이드) 파이어베이스 Realtime Database + 리싸이클러뷰 (0) | 2022.01.04 |
안드로이드) 파이어베이스 구글 로그인 (0) | 2022.01.04 |