所以我有一个从MainFragment调用的CameraFragment。问题是我使用自定义监听器将图像数据传递给MainFragment.java.Here是我如何调用CameraFragment.java
在MainFragment.java中:
public static class MainFragment extends Fragment implements CameraFragment.CameraFragmentHolder.CameraListener {
ImageView mImageView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
mImageView = (ImageView) rootView.findViewById(R.id.image_view);
return rootView;
}
private OnClickListener onCameraFragmentClicked = new OnClickListener() {
@Override
public void onClick(View v) {
FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.container, CameraFragment.CameraFragmentHolder.newInstance(MainFragmentHolder.this, 0, false));
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
};
@Override
public void onImageSaved(byte[] data) {
if (data != null) {
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
mImageView.setImageBitmap(bitmap);
}
} else {
Log.v(TAG, "Didn't get any result");
}
}
}
然后在CameraFragment.java中实现以下内容:
CameraFragment.java
private byte[] mCameraData;
private CameraListener mListener;
public interface CameraListener {
public void onImageSaved(byte[] data);
}
private OnClickListener mDoneButtonClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
if (mCameraData != null) {
removeCameraView();
sendImageDataToListener();
} else {
mCamera = null;
removeCameraView();
}
}
};
//Send image back to Listener
private void sendImageDataToListener() {
if (mCameraData != null) {
mListener.onImageSaved(mCameraData);
} else {
Log.v("CAMERA IMAGE ERROR:", "NO IMAGE DATA");
}
}
private void removeCameraView() {
getActivity().getSupportFragmentManager().popBackStack();
}
当我尝试setImageBitmap没有任何反应时,我的问题是在CallBackListener的MainFragment中。
答案 0 :(得分:1)
所以@Selvin实际上是可能的。我做的是onViewStateRestored。我读了一下android API。这不应该给任何问题,但我仍然对它持怀疑态度,因为我还没有完全测试它。我修改了MainFragment.java如下。
在MainFragment.java中:
Bitmap cameraImage;
@Override
public void onViewStateRestored(Bundle savedInstanceState) {
super.onViewStateRestored(savedInstanceState);
if (cameraImage != null) {
mImageView.setImageBitmap(cameraImage);
}
}
@Override
public void onImageSaved(byte[] data) {
if (data != null) {
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
cameraImage = bitmap;
}
} else {
Log.v(TAG, "Didn't get any result");
}