Android
안드로이드. 자식 액티비티 닫을 때, 부모 액티비티도 같이 닫기How to finish parent activity from child activity
paku_gun
2019. 4. 4. 16:52
안드로이드. 자식 액티비티 닫을 때, 부모 액티비티도 같이 닫기
How to finish parent activity from child activity
<want to move like this>
액티비티가 A -> B -> C -> D 이렇게 열릴 때,
* D 까지 왔을 때, C와 B 모두 닫기 - D에서 Back버튼으로 A로 바로 가기
* D 로 가면서 C 닫기 -> B도 함께 닫기
* parent = B, child = C
<parent activity>
Intent intent = new Intent(getApplicationContext(),childActivity.class);
startActivityForResult(intent,REQ_EXIT);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQ_EXIT){
if(resultCode == RESULT_OK){
this.finish();
}
}
}
* REQ_EXIT = code 값. int
<child activity>
intent = new Intent(getApplicationContext(),MovetoDActivity.class);
startActivity(intent);
endThisActivity();
private void endThisActivity(){
Intent intent = getIntent();
setResult(RESULT_OK, intent);
finish();
}
이런식으로 하면 D 액티비티까지 왔을 때, 스택에는
| D |
| B |
| A |
이런식으로 액티비티가 쌓여 있지만
D에서 B로 가면 바로 B가 닫히기 때문에 A로 넘어가게 되므로
D에서 A로 바로 넘어가는 것 처럼 보인다.