이것은 활동에서 중첩 된 조각으로 결과를 가져 오는 유명한 문제이며, 활동은 직접 Activity에 첨부되었지만 중첩되지 않은 단편에만 결과를 보낼 수 있습니다. 하나. 이것이 중첩 된 단편의 onActivityResult가 어쨌든 호출되지 않은 이유입니다. 제 경우에는 하나의 프래그먼트에 viewPager와 하나의 프래그먼트가 포함 된 하나의 액티비티가 있습니다. viewPager의이 조각들 중 하나에서 카메라 의도를 시작하려고합니다.이 의도로 반환 된 결과는 마지막 조각으로 보내지지 않습니다. 나는 발견 한 해답을 시도했지만 그 중 아무 것도 견고하지 않았습니다. 해결책 중 하나는 eventBus 객체를 생성하여 activity의 resultCode를 모든 조각으로 분산 시키지만이 경우에는 도움이되지 않습니다.onActivityResult가 뷰 페이지에서 중첩 된 조각에 대해 호출되도록 만드는 방법
1
A
답변
0
매우 비슷한 상황에 처해 있었기 때문에 제가 일할 해결책이 될 수도 있습니다.
간단히 말해서 은을 Fragment
의 onActivityResult()
에 의존하고 직접 작동 논리를 구현했습니다.
신뢰할 수있는 유일한 onActivityResult()
은 Activity
중 하나입니다.
이 하나
public interface ActivityResultDispatcher {
// Pass anything you want, Bundle, Intent, ecc...
// For the sake of simplicity I'm using Bundle...
public void dispatchResultData(final Bundle data);
}
같은 interface
를 작성하고 Fragment
클래스를 구현할 수있다.
당신의 Activity
사이의 통신 당신의 주Fragment
당신이 (당신의 Activity
에서) 이런 일을 할 수있는 매우 쉬워야한다는 :
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
final Bundle bundle = new Bundle();
// put data inside Bundle
this.primaryFrament.dispatchResultData(bundle);
}
, 기본 Fragment
은 보유 하나입니다 다른 Frament
클래스를 가진 ViewPager
.
Fragment
에 당신은 할 것 :
@Override public void dispatchResultData(final Bundle data) {
for (int i = 0; i < MyViewPager.NUM_FRAGMENTS; i++) {
final ActivityResultDispatcher subFragment = (ActivityResultDispatcher) this.retrieveFragment(i);
// some fragments in ViewPager may be dynamically deleted
if (subFragment != null) {
subFragment.dispatchResultData(data);
}
}
}
기능 retrieveFragment(final int position)
내가 웹에서 발견 한 코드의 아주 아늑한 작품이다.
이 같은 ViewPager
작품에 대한 구현은 다음 ViewPager
에
public Fragment retrieveFragment(final int position) {
return this.fragmentManager.findFragmentByTag("android:switcher:" + R.id.my_pager_id + ":" + position);
}
모든 활성 Fragment
클래스는 Bundle
받게됩니다 그리고 당신은 그것으로 무엇을할지 결정할 수 있습니다.
당신은
public void dispatchResultData(final Bundle data, final int requestCode);
로, 발송자에 같은 더 많은 정보를 전달하고 함수 호출에 대답해야합니다 경우 Fragment
결정이있을 수 있습니다.
기본적으로 완전 작동하는 onActivityResult()
유틸리티를 구축했으며 Fragments
(인스턴스화 된 경우)의 결과가 모두 수신됨을 확신 할 수 있습니다.