티스토리 뷰

반응형

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

반응형
댓글
공지사항
최근에 올라온 글