当相机活动返回时,承载这个片段的活动有它的onActivityResult被调用。

我的片段开始一个活动的结果与意图发送给相机拍照。图片应用程序加载正常,拍摄照片并返回。然而onActivityResult从未被击中。我设置了断点,但什么都没有触发。一个片段可以有onActivityResult吗?我想是的,因为它是一个已提供的函数。为什么这个没有被触发?

ImageView myImage = (ImageView)inflatedView.findViewById(R.id.image);
myImage.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View view) {
        Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(cameraIntent, 1888);
    }
});

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if( requestCode == 1888 ) {
        Bitmap photo = (Bitmap) data.getExtras().get("data");
        ((ImageView)inflatedView.findViewById(R.id.image)).setImageBitmap(photo);
    }
}

当前回答

You can simply override BaseActivity onActivityResult on fragment baseActivity.startActivityForResult . On BaseActivity add interface and override onActivityResult. private OnBaseActivityResult baseActivityResult; public static final int BASE_RESULT_RCODE = 111; public interface OnBaseActivityResult{ void onBaseActivityResult(int requestCode, int resultCode, Intent data); } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if(getBaseActivityResult() !=null && requestCode == BASE_RESULT_RCODE){ getBaseActivityResult().onBaseActivityResult(requestCode, resultCode, data); setBaseActivityResult(null); } On Fragment implements OnBaseActivityResult @Override public void onBaseActivityResult(int requestCode, int resultCode, Intent data) { Log.d("RQ","OnBaseActivityResult"); if (data != null) { Log.d("RQ","OnBaseActivityResult + Data"); Bundle arguments = data.getExtras(); } }

这个变通方法会奏效。

其他回答

我强烈怀疑这里所有的答案只不过是黑客。我都试过了,但没有任何可靠的结论,因为总有一些愚蠢的问题。就我个人而言,我不能依赖不一致的结果。如果你看了Fragments的官方Android API文档,你会发现谷歌清楚地陈述了以下内容:

从包含Activity的片段调用startActivityForResult(Intent, int)。

参见:Android Fragment API

因此,似乎最正确和可靠的方法是实际调用startActivityForResult()从托管活动,也从那里处理结果的onActivityResult()。

解决方案1:

调用startActivityForResult(intent, REQUEST_CODE);而不是getActivity()。startActivityForResult(意图,REQUEST_CODE);。

解决方案2:

当startActivityForResult(intent, REQUEST_CODE);调用活动的onActivityResult(requestCode,resultcode,intent),然后你可以从这里调用片段onActivityResult(),传递requestCode,resultcode和intent。

对于那些使用Android导航组件的人,应该在Activity的onActivityResult(…)中使用primaryNavigationFragment来获取它的片段引用,并调用fragment的fragment.onActivityResult(…)

下面是Activity的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);
    }
}

你的代码有一个嵌套的片段。调用超。onActivityForResult不起作用

你不希望修改你的片段可以被调用的每个活动,或者绕过调用片段链中的每个片段。

这是许多可行的解决方案之一。动态地创建一个片段,并使用支持片段管理器将其直接连接到活动。然后从新创建的片段调用startActivityForResult。

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();
}

You can simply override BaseActivity onActivityResult on fragment baseActivity.startActivityForResult . On BaseActivity add interface and override onActivityResult. private OnBaseActivityResult baseActivityResult; public static final int BASE_RESULT_RCODE = 111; public interface OnBaseActivityResult{ void onBaseActivityResult(int requestCode, int resultCode, Intent data); } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if(getBaseActivityResult() !=null && requestCode == BASE_RESULT_RCODE){ getBaseActivityResult().onBaseActivityResult(requestCode, resultCode, data); setBaseActivityResult(null); } On Fragment implements OnBaseActivityResult @Override public void onBaseActivityResult(int requestCode, int resultCode, Intent data) { Log.d("RQ","OnBaseActivityResult"); if (data != null) { Log.d("RQ","OnBaseActivityResult + Data"); Bundle arguments = data.getExtras(); } }

这个变通方法会奏效。