onActivityResult is not being called in Fragment

后端 未结 30 2701
忘了有多久
忘了有多久 2020-11-21 04:28

The activity hosting this fragment has its onActivityResult called when the camera activity returns.

My fragment starts an activity for a result with th

相关标签:
30条回答
  • 2020-11-21 05:11

    FOR MANY NESTED FRAGMENTS (for example, when using a ViewPager in a fragment)

    In your main activity:

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }
    

    In your fragment:

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        for (Fragment fragment : getChildFragmentManager().getFragments()) {
            fragment.onActivityResult(requestCode, resultCode, data);
        }
    }
    

    In your nested fragment

    Call activity

    getParentFragment().startActivityForResult(intent, uniqueInstanceInt);
    

    uniqueInstanceInt - replace it with an int that is unique among the nested fragments to prevent another fragment treat the answer.

    Receive response

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == uniqueInstanceInt ) {
            // TODO your code
        }
    }
    

    Attention

    A number between 0 and 65536 need be used in uniqueInstanceInt for error avoid "Can only use lower 16 bits for requestCode".

    0 讨论(0)
  • 2020-11-21 05:12

    In case you don't know fragments in your activity just enumerate them all and send activity result arguments:

    // In your activity
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        for (Fragment fragment : getSupportFragmentManager().getFragments()) {
            fragment.onActivityResult(requestCode, resultCode, data);
        }
    }
    
    0 讨论(0)
  • 2020-11-21 05:12

    FOR NESTED FRAGMENTS (for example, when using a ViewPager)

    In your main activity:

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }
    

    In your main top level fragment(ViewPager fragment):

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        YourFragment frag = (YourFragment) getChildFragmentManager().getFragments().get(viewPager.getCurrentItem());
        frag.yourMethod(data);  // Method for callback in YourFragment
        super.onActivityResult(requestCode, resultCode, data);
    }
    

    In YourFragment (nested fragment):

    public void yourMethod(Intent data){
        // Do whatever you want with your data
    }
    
    0 讨论(0)
  • 2020-11-21 05:13

    Option 1:

    If you're calling startActivityForResult() from the fragment then you should call startActivityForResult(), not getActivity().startActivityForResult(), as it will result in fragment onActivityResult().

    If you're not sure where you're calling on startActivityForResult() and how you will be calling methods.

    Option 2:

    Since Activity gets the result of onActivityResult(), you will need to override the activity's onActivityResult() and call super.onActivityResult() to propagate to the respective fragment for unhandled results codes or for all.

    If above two options do not work, then refer to option 3 as it will definitely work.

    Option 3:

    An explicit call from fragment to the onActivityResult function is as follows.

    In the parent Activity class, override the onActivityResult() method and even override the same in the Fragment class and call as the following code.

    In the parent class:

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.dualPane);
        fragment.onActivityResult(requestCode, resultCode, data);
    }
    

    In the child class:

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // In fragment class callback
    }
    
    0 讨论(0)
  • 2020-11-21 05:14

    For those who use Android Navigation Component should use in Activity's onActivityResult(...) the primaryNavigationFragment to get it's fragment reference and call fragment's fragment.onActivityResult(...).

    Here's Activity's onActivityResult(...)

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent imageData)
    {
        super.onActivityResult(requestCode, resultCode, imageData);
    
        for (Fragment fragment : getSupportFragmentManager().getPrimaryNavigationFragment().getChildFragmentManager().getFragments())
        {
                fragment.onActivityResult(requestCode, resultCode, imageData);
        }
    }
    
    0 讨论(0)
  • 2020-11-21 05:15

    If the above problem is faced at Facebook login then you can use the below code in a parent activity of your fragment like:

    Fragment fragment = getFragmentManager().findFragmentById(android.R.id.tabcontent);
    fragment.onActivityResult(requestCode, resultCode, data);
    

    Or:

    Fragment fragment = getFragmentManager().findFragmentById("fragment id here");
    fragment.onActivityResult(requestCode, resultCode, data);
    

    And add the below call in your fragment...

    callbackManager.onActivityResult(requestCode, resultCode, data);
    
    0 讨论(0)
提交回复
热议问题