bboks.net™

안드로이드 back 버튼 종료여부 묻기 본문

Mobile Programming/Android

안드로이드 back 버튼 종료여부 묻기

bboks.net 2014. 4. 2. 17:21
방법1
@Override
 public boolean onKeyDown(int keyCode, KeyEvent event) {
 	 switch(keyCode){
      	case KeyEvent.KEYCODE_BACK:
        String alertTitle = getResources().getString(R.string.app_name); // alert 타이틀
        String buttonMessage = getResources().getString(R.string.msg); // alert 메시지
        String buttonYes = getResources().getString(R.string.btn_yes);   // yes버튼 글자
        String buttonNo = getResources().getString(R.string.btn_no);      // no버튼 글자
           
        Builder builder = new AlertDialog.Builder(this);
        		  builder.setTitle(alertTitle);
        		  builder.setMessage(buttonMessage);
        		  builder.setNegativeButton(buttonNo, null);
        		  builder.setPositiveButton(buttonYes, new DialogInterface.OnClickListener() {
      	           @Override
   	           public void onClick(DialogInterface dialog, int which) {
   	            // TODO Auto-generated method stub
   	            moveTaskToBack(true);
   	            finish();
   	           }
        		  });
        
        		  builder.show();
      }
     return true;
 }

방법2 (2.x 이상)
@Override
   public void onBackPressed() {
    String alertTitle = getResources().getString(R.string.app_name);
    String buttonMessage = getResources().getString(R.string.msg);
    String buttonYes = getResources().getString(R.string.btn_yes);
    String buttonNo = getResources().getString(R.string.btn_no);
     
    Builder builder = new AlertDialog.Builder(this);
  	  	    builder.setTitle(alertTitle);
  		    builder.setMessage(buttonMessage);
  		    builder.setNegativeButton(buttonNo, null);
  		    builder.setPositiveButton(buttonYes, new DialogInterface.OnClickListener() {
   	           @Override
	           public void onClick(DialogInterface dialog, int which) {
	            // TODO Auto-generated method stub
	            moveTaskToBack(true);
	            finish();
	           }
  		    });
  
  		    builder.show();
   }


[출처] back버튼 눌렀을때 종료여부 묻기