我正在尝试将byteArray和一个String从当前活动传递到上一个片段。
public void uploadClick (View view) {
//Send results back to previous screen
switch (activity.from) {
case "review" :
if (activity.mCurrentPhotoPath != null && activity.bytes != null) {
Intent reviewReturnIntent = new Intent();
reviewReturnIntent.putExtra(ReviewsFragment.REVIEW_PHOTO_BITMAP, activity.bytes);
reviewReturnIntent.putExtra(ReviewsFragment.REVIEW_PHOTO_PATH, activity.mCurrentPhotoPath);
activity.setResult(Activity.RESULT_OK, reviewReturnIntent);
activity.finish();
}
break;
case "reply" :
break;
case "merchant" :
break;
}
}
onActivityResult片段:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REVIEW_REQUEST) {
if (resultCode == getActivity().RESULT_OK) {
if (data != null) {
returnedBitmap = data.getByteArrayExtra(REVIEW_PHOTO_BITMAP);
returnedPhotoPath = data.getStringExtra(REVIEW_PHOTO_PATH);
Bitmap bmp = BitmapFactory.decodeByteArray(returnedBitmap, 0, returnedBitmap.length);
binding.reviewIV.setImageBitmap(bmp);
binding.returnedImageLayout.setVisibility(View.VISIBLE);
}
}
}
}
startActivityForResult调用:
public void uploadClick (View view) {
Intent uploadPhotoIntent = new Intent(fragment.getContext(), UploadPhotoActivity.class);
uploadPhotoIntent.putExtra(UploadPhotoActivity.FROM, "review");
fragment.startActivityForResult(uploadPhotoIntent, ReviewsFragment.REVIEW_REQUEST);
}
每次我尝试使用intent参数运行activity.setResult时。编译器将运行该代码,但不会完成该活动(因此,我无法返回上一个片段/活动)。 我添加了断点,它运行“ setResult”和“ activity.finish()”方法。但这似乎并没有破坏当前的活动。
尽管从“ setResult”方法中删除了intent参数后,一切正常,但是在上一个片段中,我从“数据”中返回了null。
示例:
activity.setResult(Activity.RESULT_OK);
曾经试图在互联网上寻找答案,但还是没有运气。 非常感谢,并感谢您的阅读。 :)