片段中的onActivityResult未被称为childFragment

时间:2016-09-05 09:52:13

标签: android android-fragments android-intent onactivityresult

我有HomeActivity在viewpager中包含许多片段。现在我有2个片段

  1. AddBalanceFragment

  2. AddSubscriptionFragment

  3. 我从两个片段调用onActivityResult,两者都有相同的代码。 onActivityResult首先转到HomeActivity的onActivityResult正常工作AddBalanceFragment它会转到addBalanceFragment的onActivityResult,但当我尝试与AddSubscriptionFragment相同时 它也适用于AddBalanceFragment的onActivityResult

    //nestedfragments contains 4 fragments   3rd is addBalanceFragment and 4th AddsubsprtionFragment
    
    
    // HomeActivity.java
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.e(TAG, "onActivityResult");
        // notifying nested fragments (support library bug fix)
        final FragmentManager childFragmentManager = getSupportFragmentManager();
    
        if (childFragmentManager != null) {
            final List<Fragment> nestedFragments = childFragmentManager.getFragments();
    
            if (nestedFragments == null || nestedFragments.size() == 0) return;
    
            for (Fragment childFragment : nestedFragments) {
                if (childFragment != null && !childFragment.isDetached() && !childFragment.isRemoving()) {
    
                    childFragment.onActivityResult(requestCode, resultCode, data);
                }
            }
        }
    }
    

    AddBalanceFragment.java

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
    
    
        if (requestCode == 1001) {
            if (resultCode == -1) {
             }
           }
    

    AddSubscriptionFragment.java

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
    
    
        if (requestCode == 1001) {
            if (resultCode == -1) {
             }
           }
    

2 个答案:

答案 0 :(得分:1)

我用静态变量状态得到它:)

toggleClass("show");

答案 1 :(得分:0)

你能确定你在打电话吗

super.onActivityResult(requestCode, resultCode, data);

on fragment的onActivityResult方法。

您甚至不需要在活动中手动调用这些方法