
액티비티를 호출하고 결과를 받아오기 위해 사용하는 startActivityForResult는 Deprecated되어 사용이 권장되지 않는다.
대신에 ActivityResultLauncher를 사용해 해당 기능을 구현할 수 있다.
ActivityResultLauncher 객체를 만든 후 registerForActivityResult를 사용해 콜백 메소드를 등록해준다.
private ActivityResultLauncher<Intent> makeResultLauncher;
...
makeResultLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult o) {
if(o.getResultCode() == RESULT_OK){
...
}
}
}
});
ActivityResultContracts 를 통해 결과를 생성하는 데 필요한 입력 유형과 결과의 출력 유형을 정의할 수 있다. 여기서는 startActivityForResult와 같은 사용법을 위해 StartActivityForResult()로 지정하였다.
그 후 callBack 메소드를 만들고 resultCode를 참조해 결과를 받아올때의 코드를 작성한다.
requestCode는 사용되지 않는다.
액티비티를 호출할 때는 다음과 같이 사용한다.
Intent intent = new Intent(getApplicationContext(), AddActivity.class);
makeResultLauncher.launch(intent);