从ViewPager的子片段调用startActivityResult()时,不会调用onActivityResult()

问题描述:

我已经阅读了相关的帖子.在每个问题中,我都看到至少调用了活动的onActivityResult().问题是如何将结果携带到片段或嵌套片段中.但是我面临的问题是不同的.
我正在实现设置"页面,该页面将具有来自新的设计支持"库的带有TabLayout的选项卡.在我的应用程序中,我只有一个Activity来保存其他片段...所以我添加了一个SettingsTabFragment,其中包含ViewPager和FragmentPagerAdapter.此适配器包含每个选项卡的片段.
选项卡之一是用户个人资料"设置,用户可以在其中添加他的个人资料图片.然后我从带有ACTION_PICK意图的子片段中调用了startActivityResult().我在Activity,父片段(包含视图分页器)和SettingsTabFragment中覆盖了onActivityResult().但是当我从画廊中选择这张照片时,他们都没有被叫到.

I have read through the related posts. In every quaestion I see that onActivityResult() of activity gets called at least..the problem is how to carry the result to the fragments or nested fragments. But issue I am facing is different.
I am implementing Settings page that will have tabs with TabLayout from new Design Support library. In my app I have a single Activity which holds other Fragments...so I added a SettingsTabFragment which has ViewPager and a FragmentPagerAdapter to it.This adapter holds fragments for each tab.
One of the tab is User Profile settings where user can add his profile pic. And I called startActivityResult() from child fragment with ACTION_PICK intent. I overriden the onActivityResult() in Activity, in parent fragment (that holds view pager) and in SettingsTabFragment too. But none of them get called when I chose the pic from gallery.

当从以下位置调用startActivityResult()时,不会调用onActivityResult(): -子片段.
-父片段.我是从子片段frag getParentFragment().startActivityResult()以及从直接连接到活动(不嵌套)的父片段本身中进行此操作的. -使用getParentFragment().getActivity().startActivityResult()使用子片段中的活动进行调用.

None of onActivityResult() get called when startActivityResult() called from:
- child fragment.
- parent fragment. I did this from child frag getParentFragment().startActivityResult() AND also from within the parent fragment itself which is attached to activity directly (not nested).
- called using activity from child fragment with getParentFragment().getActivity().startActivityResult().

它们都不导致至少对活动的onActivityResult()的调用.

None of them lead to call onActivityResult() of, at least, activity.

当我从片段中调用startActivityResult()时,我确保只调用了startActivityResult().. not getActivity().startActivityResult().

When I called startActivityResult() from fragment I made sure I called just startActivityResult()..not getActivity().startActivityResult().

我追踪 onActivityResult未在Fragment中调用,并尝试了github lib在答案之一中建议,但似乎所有者告诉我们现在不建议这样做.也有编译错误.

I follwed onActivityResult is not being called in Fragment and tried the github lib suggested in one of the answers but seems owner told it is not recommended now. Also it has got compilation errors.

不是对此有想法吗?

在我的情况下,我正在通过使用我的子片段类中的intent(即视图分页器的选项卡之一)从联系人那里获取手机号码.

In my case i was picking up the mobile number from the contact by using intent in my child fragment class i.e. one of tab of view pager.

从MainActivity调用父片段:

//Home是我的父片段,其中包含Tab布局和View Pager.我通过指定标签来调用Home Fragment,稍后我将使用此标签.

// Home is my parent fragment which contains Tab Layout and View Pager. I am calling Home Fragment by specifying tag, i will use this tag later on.

Fragment fragment = new Home();
fragmentManager.beginTransaction()
               .replace(R.id.main_content, fragment, "MobileTag")
               .addToBackStack(null)
               .commit();

在我的孩子片段"课程中:

Intent intent = new Intent(Intent.ACTION_PICK, 
                           ContactsContract.Contacts.CONTENT_URI);
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(intent, 1);

现在它将调用MainActivity类的onActivityResult方法 在主要活动中:

Now it will call the onActivityResult method of MainActivity class In Main Activity:

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
}

通过在MainActivity的onActivityResult中调用super将调用父片段的i.e.,即Home Fragment的onActivityResult方法.

By Calling super in onActivityResult of MainActivity will make a call to Parent fragment's i.e. Home Fragment's onActivityResult method.

在父片段"(即家庭片段")中

In Parent Fragment i.e. Home Fragment:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    Fragment fragment = getFragmentManager()
        .findFragmentByTag("MobileTag")
        .getChildFragmentManager()
        .getFragments()
        .get(0);
    fragment.onActivityResult(requestCode, resultCode, data);
}

在这里,我得到了父片段的第0个子元素,例如在查看寻呼机适配器"中是否添加了片段,如:

here I am getting the 0th Child of parent fragment e.g. In View Pager Adapter if Fragment are added like:

@Override
public Fragment getItem(int position) {
    switch (position){
        case 0 :return new ChildFragment0();
        case 1 : return new ChildFragment1();
        case 2 : return new ChildFragment2();
    }
    return null;
}

然后

getFragmentManager()
   .findFragmentByTag("MobileTag")
   .getChildFragmentManager()
   .getFragments()
   .get(0); // will return ChildFragment0

此后,调用子片段的onActivityResult方法:

After this Make a call to child fragment's onActivityResult method:

最后是子片段:

public void onActivityResult(int reqCode, int resultCode, Intent data) {
    Cursor cursor = null;
    String contactNumber = "";           
    Uri result = data.getData();
    // get the result here
}