onActivityResult is not being called in Fragment

后端 未结 30 2718
忘了有多久
忘了有多久 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: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
    }
    

提交回复
热议问题