티스토리 뷰
Stack Overflow에 자주 검색, 등록되는 문제들과 제가 개발 중 찾아 본 문제들 중에서 나중에도 찾아 볼 것 같은 문제들을 정리하고 있습니다.
Stack Overflow에서 가장 먼저 확인하게 되는 가장 높은 점수를 받은 Solution과 현 시점에 도움이 될 수 있는 가장 최근에 업데이트(최소 점수 확보)된 Solution을 각각 정리하였습니다.
아래 word cloud를 통해 이번 포스팅의 주요 키워드를 미리 확인하세요.

onActivityResult is not being called in Fragment
onActivityResult가 Fragment에서 호출되지 않습니다
문제 내용
The activity hosting this fragment has its onActivityResult
called when the camera activity returns.
이 Fragment를 호스팅하는 액티비티는 카메라 액티비티에서 돌아오면 onActivityResult 결과에 대해 호출됩니다.
My fragment starts an activity for a result with the intent sent for the camera to take a picture. The picture application loads fine, takes a picture, and returns. The onActivityResult
however is never hit. I've set breakpoints, but nothing is triggered. Can a fragment have onActivityResult
? I'd think so since it's a provided function. Why isn't this being triggered?
내 Fragment는 카메라가 사진을 찍기 위해 전송된 intent와 함께 결과를 위한 액티비티를 시작한다. 사진 응용 프로그램이 정상적으로 로드되고 사진을 찍은 후 반환됩니다. 그러나 onActivityResult는 불리지 않습니다. 중단점을 설정했지만 트리거된 것은 없습니다. Fragment가 onActivityResult를 가질 수 있나요? 제공되는 기능이기 때문에 그렇게 생각합니다. 왜 이것이 작동되지 않는 거죠?
ImageView myImage = (ImageView)inflatedView.findViewById(R.id.image);
myImage.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, 1888);
}
});
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if( requestCode == 1888 ) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
((ImageView)inflatedView.findViewById(R.id.image)).setImageBitmap(photo);
}
}
높은 점수를 받은 Solution
The hosting activity overrides onActivityResult()
, but it did not make a call to super.onActivityResult()
for unhandled result codes. Apparently, even though the fragment is the one making the startActivityForResult()
call, the activity gets the first shot at handling the result. This makes sense when you consider the modularity of fragments. Once I implemented super.onActivityResult()
for all unhandled results, the fragment got a shot at handling the result.
호스팅 액티비티는 onActivityResult()를 재정의하지만 처리되지 않은 결과 코드에 대해 super.onActivityResult()를 호출하지 않았습니다. 분명히, Fragment가 startActivityForResult() 호출을 하는 것이지만, 액티비티는 결과를 처리할 때 첫 번째 기회를 얻는다. 이것은 Fragment의 모듈화를 고려할 때 타당하다.
저는 처리되지 않은 모든 결과에 대해 super.onActivityResult()를 구현해 Fragment가 이 결과를 처리할 기회를 얻었다.
And also from @siqing answer:
또한 @siqing에서 다음과 같이 대답합니다.
To get the result in your fragment make sure you call startActivityForResult(intent,111);
instead of getActivity().startActivityForResult(intent,111);
inside your fragment.
Fragment의 결과를 얻으려면 getActivity().startActivityForResult(intent,111) 대신 startActivityForResult(intent,111)를 호출해야 합니다.
가장 최근 달린 Solution
Kotlin version for those who use Android Navigation Component inspired in Mohit Mehta's answer
모히트 메타의 답변에서 영감을 받은 안드로이드 내비게이션 컴포넌트를 사용하는 사람들을 위한 코틀린 버전.
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
supportFragmentManager.primaryNavigationFragment?.childFragmentManager?.fragments?.forEach { fragment ->
fragment.onActivityResult(requestCode, resultCode, data)
}
}
출처 : https://stackoverflow.com/questions/6147884/onactivityresult-is-not-being-called-in-fragment
'개발 > 안드로이드' 카테고리의 다른 글
intent를 사용하여 다른 액티비티로 object를 보내는 방법 (0) | 2022.11.30 |
---|---|
Android에서 액티비티가 시작될 때 EditText에 포커스 가는 것 막기 (0) | 2022.11.30 |
WebView에서 뒤로 버튼을 눌렀을 때 이전 페이지로 돌아가는 방법 (0) | 2022.11.29 |
안드로이드 인텐스에서 추가 데이터를 얻으려면 어떻게 해야 하나요? (0) | 2022.11.29 |
WebView에서 URL 로드가 끝난 시점 확인하기 (0) | 2022.11.29 |