마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

경고 팝업에서 앱을 종료 시키는 방법이 궁금합니다.

0 추천
서버와 연결이 끊겼을 경우

 

확인 버튼이 있는 경고 팝업을 띄워 주고

 

onclick()에서 finish()만 하게 되면 해당 팝업만 종료가 되는데

앱 전체를 종료시킬수 있는 방법이 궁금합니다.
tatwolf (300 포인트) 님이 2014년 6월 10일 질문

3개의 답변

+1 추천
 
채택된 답변
 
void android.app.Activity.finishAffinity()

 

Finish this activity as well as all activities immediately below it in the current task that have the same affinity. This is typically used when an application can be launched on to another task (such as from an ACTION_VIEW of a content type it understands) and the user has used the up navigation to switch out of the current task and in to its own task. In this case, if the user has navigated down into any other activities of the second application, all of those should be removed from the original task as part of the task switch.

Note that this finish does not allow you to deliver results to the previous activity, and an exception will be thrown if you are trying to do so.

쭈쭈총각 (17,750 포인트) 님이 2014년 6월 12일 답변
tatwolf님이 2014년 6월 12일 채택됨
+1 추천

저 같은 경우엔 로그아웃기능을 만들어서 어느 화면에서든지

로그아웃 하게 될경우 전체 Activity 들은 종료하도록 구현을 했었는데요.

 

Activity 스택에는 언제나 MainActivity가 최하위에 있다는 가정하에 

Intent intent = new Intent(context, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra(EXITKEY, EXIT);
		
startActivity(intent);

위처럼 호출하면, MainActivity 부터 context까지의 모든 Activity들은 자동으로 종료되고, 

MainActivity에서는 getIntent() 함수를 아래처럼 오버라이딩하여

 

        @Override
	public Intent getIntent() {
		Intent intent = super.getIntent(); 
		int exitval = intent.getIntExtra(EXITKEY, 1);
		
		if(exitval == EXIT || exitval == LOGOUT){
			this.finish();
		}
		
		
		return intent;
	}

로그아웃 기능을 구현 했습니다. 

엡뽀 (160 포인트) 님이 2014년 6월 10일 답변
0 추천
System.exit(0) ...

저 같은 경우엔 Activity를 상속하여 Receiver를 등록하는 클래스를 만들고 ...

액티비티마다 위의 클래스를 상속해서 Broadcast를 날려버립니다.

 

Broadcast를 사용못하겠다고 한다면, static으로 Activity를 저장하는 List에 차곡차곡 Activity를 쌓아두다가

다 종료시키고 싶으면 하나하나 finish 시키는것도 괜찮을 것 같네요.
북극토끼 (420 포인트) 님이 2014년 6월 10일 답변
북극토끼님이 2014년 6월 10일 수정
...