我有一个活动需要触发4个不同的DialogFragments,其中我触发一个意图从相机或图库中获取图像。 我可以保留片段状态并继续观察“onActivityResult”并将其发送到dialogfragmentx但是当它们重新创建并且我尝试再次显示我从图库中获得的图像时,当我在线性布局上使用“addView”时它会没有。 以下是我的活动代码:
private DFrag_room frag_room;
private DFrag_terrace frag_terrace;
private DFrag_garden frag_garden;
private DFrag_parking frag_parking;
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if (frag_room != null) {
getSupportFragmentManager().putFragment(outState, "fragment_room", frag_room);
}
if (frag_terrace != null) {
getSupportFragmentManager().putFragment(outState, "fragment_terrace", frag_terrace);
}
if (frag_garden != null) {
getSupportFragmentManager().putFragment(outState, "fragment_garden", frag_garden);
}
if (frag_parking != null) {
getSupportFragmentManager().putFragment(outState, "fragment_parking", frag_parking);
}
}
@Override
public void onRestoreInstanceState(Bundle inState){
FragmentManager fm = getSupportFragmentManager();
frag_room = (DFrag_room) fm.getFragment(inState, "fragment_room");
frag_terrace = (DFrag_terrace) fm.getFragment(inState, "fragment_terrace");
frag_garden = (DFrag_garden) fm.getFragment(inState, "fragment_garden");
frag_parking = (DFrag_parking) fm.getFragment(inState, "fragment_parking");
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (frag_room != null) {
frag_room.onActivityResult(requestCode, resultCode, data);
}
if (frag_terrace != null) {
frag_terrace.onActivityResult(requestCode, resultCode, data);
}
if (frag_garden != null) {
frag_garden.onActivityResult(requestCode, resultCode, data);
}
if (frag_parking != null) {
frag_parking.onActivityResult(requestCode, resultCode, data);
}
}
和其中一个对话片段的代码(每个程序的步骤相同)
私人LinearLayout layout_images; 私人ImageView img_add_image;
private File photoFile;
private ArrayList<Uri> images;
public DFrag_room() {
}
public interface DFrag_roomListener {
void onFinishRoom();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dfrag_room, container);
layout_images = (LinearLayout) view.findViewById(R.id.dfrag_room_imgs_layout);
img_add_image = (ImageView) view.findViewById(R.id.dfrag_room_img_add_img);
img_add_image.setOnClickListener(this);
if (savedInstanceState != null) {
images = (ArrayList<Uri>) savedInstanceState.getSerializable("images");
for (int i = 0; i < images.size(); i++) {
addImage(images.get(i));
}
}
setRetainInstance(true);
return view;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
return dialog;
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState); // <-- must call this if you want to retain dialogFragment upon rotation
outState.putSerializable("images", images);
}
@Override
public void onDestroyView() {
if (getDialog() != null && getRetainInstance())
getDialog().setDismissMessage(null);
super.onDestroyView();
}
@Override
public void onActivityResult(int reqCode, int resCode, Intent data) {
switch (reqCode) {
case CONF.REQ_PHOTO:
if (resCode == Activity.RESULT_OK) {
if ((photoFile != null)) {
Uri img = Uri.fromFile(photoFile);
images.add(img);
addImage(img);
}
}
break;
case CONF.REQ_GALLERY:
if (resCode == Activity.RESULT_OK) {
images.add(data.getData());
addImage(data.getData());
}
break;
}
super.onActivityResult(resCode, resCode, data);
}
public void addImage(Uri img_uri) {
int margin = getResources().getDimensionPixelOffset(R.dimen.padding_x_small);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(img_add_image.getWidth(), img_add_image.getHeight());
params.setMargins(0, 0, margin, 0);
RoundedImageView new_img = new RoundedImageView(getActivity());
new_img.setLayoutParams(params);
new_img.setScaleType(ImageView.ScaleType.CENTER_CROP);
new_img.setBackgroundResource(R.drawable.bg_white_stroke);
new_img.setCornerRadius((float) 6);
Glide.with(getActivity()).load(img_uri).thumbnail(0.8f).into(new_img);
layout_images.addView(new_img, 0);
}
当我从意图中获取图像时,它工作正常,但是当我从保存的uris列表(onCreateView)中获取它时,它无法正常工作
编辑: 对不起,应用程序没有崩溃,我没有看到我的布局中的图像,它调用“addImage”,但我没有看到图像。