본문 바로가기

Android

Android Studio BackButton 두번 클릭시 종료 설정.

반응형

private long pressedTime;

 

    @Override

    public void onBackPressed() {

        //super.onBackPressed();

        if ( pressedTime == 0 ) {

            Toast.makeText(MainActivity.this"한번 더 누르면 종료됩니다", Toast.LENGTH_LONG).show();

            pressedTime = System.currentTimeMillis();

        }

        else {

            int seconds = (int) (System.currentTimeMillis() - pressedTime);

 

            if ( seconds > 2000 ) {

                pressedTime = 0;

            }

            else {

                finish();

            }

        }

    }

 

반응형