결과화면
MainBackPressCloseHandler.class 생성
import android.app.Activity;
import android.widget.Toast;
public class MainBackPressCloseHandler {
private long backKeyPressedTime = 0;
private Toast toast;
private Activity activity;
public MainBackPressCloseHandler(Activity context) {
this.activity = context;
}
public void onBackPressed() {
if (System.currentTimeMillis() > backKeyPressedTime + 2000) {
backKeyPressedTime = System.currentTimeMillis();
showGuide();
return;
}
if (System.currentTimeMillis() <= backKeyPressedTime + 2000) {
activity.finish();
android.os.Process.killProcess(android.os.Process.myPid());
toast.cancel();
}
}
public void showGuide() {
toast = Toast.makeText(activity, "\'뒤로\'버튼을 한번 더 누르시면 종료됩니다.",
Toast.LENGTH_SHORT);
toast.show();
}
}
private MainBackPressCloseHandler mainBackPressCloseHandler ;
사용하고 싶은 페이지에
MainBackPressCloseHandler 선언
mainBackPressCloseHandler = new MainBackPressCloseHandler(this);
객체 생성
@Override
public void onBackPressed() {
mainBackPressCloseHandler.onBackPressed();
}
onBackPressed() 오버라이드
MainActivity.class
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.ArrayList;
public class MainActivity extends Activity {
private MainBackPressCloseHandler mainBackPressCloseHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainBackPressCloseHandler = new MainBackPressCloseHandler(this);
}
@Override
public void onBackPressed() {
mainBackPressCloseHandler.onBackPressed();
}
}
'Android' 카테고리의 다른 글
[Android] 안드로이드 JSON 문자열 파싱(parsing) (0) | 2017.07.18 |
---|---|
[Android] 안드로이드 내 휴대폰(디바이스) 번호 가져오기 (5) | 2017.07.18 |
[Android] 안드로이드 Splash Activity (어플 설명 액티비티, 어플 대표 화면) 만들기 (4) | 2017.07.13 |
[Android] 안드로이드 <-> Servlet <-> Mysql 연동 (3) - Android, Servlet 연동 (16) | 2017.07.13 |
[Android] ImageView 숨기기(안보이게) 기능 (0) | 2017.07.10 |