onActivityResult is not being called in Fragment

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

提交回复
热议问题