当相机活动返回时,承载这个片段的活动有它的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(); } }

这个变通方法会奏效。

其他回答

添加这个

public void onClick(View v) {   
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT,MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(intent, 1);
}

当你将替换你的代码与上述代码,然后自动你的这个

public void onActivityResult(int requestCode, int resultCode,
@Nullable Intent data){}

方法将开始工作

//No Need to write this code in onclick method
    Intent intent=new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT)
    startActivityForResult(intent,1);
    Toast.makeText(getContext(), "image"+intent, Toast.LENGTH_SHORT).show();

最简单的方法之一是从你的片段开始一个活动。

startActivity(ActivityName);

然后,添加你调用startActivityForResult(intent,"1");在你的Activity中添加onActivityResult

startActivityForResult(intent,"1");

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.dualPane);
    fragment.onActivityResult(requestCode, resultCode, data);
// perform other activity result like fetching from Uris or handling cursors

finish(); // finish the activity will  get to back to your fragment.
}

选项1:

如果你从片段调用startActivityForResult(),那么你应该调用startActivityForResult(),而不是getActivity().startActivityForResult(),因为它将导致片段onActivityResult()。

如果你不确定你在哪里调用startActivityForResult()和你将如何调用方法。

选项2:

由于Activity获得onActivityResult()的结果,您将需要覆盖活动的onActivityResult()并调用super.onActivityResult()来传播到未处理的结果代码或所有的各自片段。

如果以上两个选项都不管用,那么请参考选项3,因为它肯定会起作用。

选项3:

从fragment显式调用onActivityResult函数如下所示。

在父Activity类中,重写onActivityResult()方法,甚至在Fragment类中重写同样的方法,并像下面的代码一样调用。

在父类中:

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

在儿童班:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // In fragment class callback
}

Kotlin版本(在你的活动onActivityResult()中)

 override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    //add following lines in your activity
    if(supportFragmentManager?.fragments!=null && supportFragmentManager?.fragments!!.size>0)
     for (i in 0..supportFragmentManager?.fragments!!.size-1) {
         val fragment= supportFragmentManager?.fragments!!.get(i)
         fragment.onActivityResult(requestCode, resultCode, data)
    }
 }

只需简单地调用:

startActivityForResult(intent, "1");