onActivityResult is not being called in Fragment

后端 未结 30 2698
忘了有多久
忘了有多久 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 04:48

    I have a strong suspicion that all of the answers here are nothing more than hacks. I've tried them all and many others, but without any reliable conclusion as there is always some sort of stupid issue. I for one cannot rely on inconsistent results. If you look at the official Android API documentation for Fragments you will see Google clearly states the following:

    Call startActivityForResult(Intent, int) from the fragment's containing Activity.

    See: Android Fragment API

    So, it would seem that the most correct and reliable approach would be to actually call startActivityForResult() from the hosting activity and also handle the resulting onActivityResult() from there.

    0 讨论(0)
  • 2020-11-21 04:49

    Another use case not already described in other answers:

    onActivityResult() declared in fragment is not invoked when using exception.startResolutionForResult():

    if (exception is ResolvableApiException) {
        exception.startResolutionForResult(activity!!, MY_REQUEST_CODE)
    }
    

    In this case replace exception.startResolutionForResult() with fragment's startIntentSenderForResult():

    if (exception is ResolvableApiException) {
        startIntentSenderForResult(exception.resolution.intentSender, MY_REQUEST_CODE, null, 0, 0, 0, null)
    }
    
    0 讨论(0)
  • 2020-11-21 04:49

    As Ollie C mentioned, there is an active bug for the support library using returned values to onActivityResult when you are using nested fragments. I just hit it :-(.

    See Fragment.onActivityResult not called when requestCode != 0.

    0 讨论(0)
  • 2020-11-21 04:50

    In short,

    In fragment, declare Fragment fragment = this;

    after that use fragment.startActivityForResult.

    The result will return in activityResult.

    0 讨论(0)
  • 2020-11-21 04:52

    I also met this problem in a Fragment. And I called startActivityForResult in a DialogFragment.

    But now this problem has been resolved:
    FragmentClassname.this.startActivityForResult.

    0 讨论(0)
  • 2020-11-21 04:52

    Your code has a nested fragment. Calling super.onActivityForResult doesn't work

    You don't want to modify every activity that your fragment can be called from and or make a work around calling every fragment in the fragment chain.

    Here is one of many working solutions. create a fragment on the fly and wire it directly to the activity with the support fragment manager. Then call startActivityForResult from the newly created fragment.

    private void get_UserEmail() {
    
        if (view == null) {
            return;
        }
        ((TextView) view.findViewById(R.id.tvApplicationUserName))
                .setText("Searching device for user accounts...");
    
        final FragmentManager fragManager = getActivity().getSupportFragmentManager();
    
        Fragment f = new Fragment() {
            @Override
            public void onAttach(Activity activity) {
                super.onAttach(activity);
                startActivityForResult(AccountPicker.newChooseAccountIntent(null, null,
                        new String[]{"com.google"}, false, null, null, null, null), REQUEST_CODE_PICK_ACCOUNT);
            }
    
            @Override
            public void onActivityResult(int requestCode, int resultCode,
                                         Intent data) {
                if (requestCode == REQUEST_CODE_PICK_ACCOUNT) {
                    String mEmail = "";
                    if (resultCode == Activity.RESULT_OK) {
                        if (data.hasExtra(AccountManager.KEY_ACCOUNT_NAME)) {
                            mEmail = data
                                    .getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
                        }
                    }
                    if (mActivity != null) {
                        GoPreferences.putString(mActivity, SettingApplication.USER_EMAIL, mEmail);
                    }
                    doUser();
                }
                super.onActivityResult(requestCode, resultCode, data);
                fragManager.beginTransaction().remove(this).commit();
            }
        };
        FragmentTransaction fragmentTransaction = fragManager
                .beginTransaction();
        fragmentTransaction.add(f, "xx" + REQUEST_CODE_PICK_ACCOUNT);
        fragmentTransaction.commit();
    }
    
    0 讨论(0)
提交回复
热议问题