片段在活动结果调用后关闭

时间:2014-03-07 04:53:43

标签: android android-fragments

我有一个片段A和B. A包含一个列表,B有一个imageview。当我点击片段A中的列表项时,它会转到B.我正在从B调用相机和图库意图。

在B

alert.setAdapter(adapter, new DialogInterface.OnClickListener() {

   public void onClick(DialogInterface dialog, int item) {
    if (item == 0) {
     Intent cameraIntent = new Intent(
       android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
     startActivityForResult(cameraIntent, PICK_FROM_CAMERA);
    } else if (item == 1) {
     Intent intent = new Intent();
     intent.setAction(Intent.ACTION_GET_CONTENT);
     intent.addCategory(Intent.CATEGORY_OPENABLE);
     intent.setType("image/*");
     startActivityForResult(intent, PICK_FROM_FILE);
    } else {
     dialog.cancel();
    }

   }
  });

B中的onActivityResult

 public void onActivityResult(int requestCode, int resultCode, Intent data) {
  super.onActivityResult(requestCode, resultCode, data);
  if (requestCode == PICK_FROM_CAMERA) {
   if (resultCode == getActivity().RESULT_OK) {
    bitmap = (Bitmap) data.getExtras().get("data");
    cameraIcon.setImageBitmap(bitmap);

   } else if (resultCode == getActivity().RESULT_CANCELED) {
    Toast.makeText(getActivity(), "Result has been cancelled!",
      Toast.LENGTH_LONG).show();
   }
  } else if (requestCode == PICK_FROM_FILE) {
   try {
    if (resultCode == getActivity().RESULT_OK) {
     try {
      stream = getActivity().getContentResolver()
        .openInputStream(data.getData());
     } catch (FileNotFoundException e) {
      e.printStackTrace();
     }
     bitmap = BitmapFactory.decodeStream(stream);
     cameraIcon
       .setImageBitmap(Bitmap.createScaledBitmap(bitmap,
         bitmap.getWidth() / 2,
         bitmap.getHeight() / 2, true));

    } else if (resultCode == getActivity().RESULT_CANCELED) {
     Toast.makeText(getActivity(), "Result has been cancelled!",
       Toast.LENGTH_LONG).show();

    }
   } catch (Exception e) {
   }
  }
 }

但是有些时候意图收集图像后来到onActivityResult片段B 关闭并转到片段A而不是将图像设置为片段B中的图像视图.....

我做错了什么,请帮忙

2 个答案:

答案 0 :(得分:1)

请使用其他设备检查。我认为设备问题。

答案 1 :(得分:0)

只是一个猜测,但我会说onActivityResult完成后片段B中会抛出一个异常。也许你应该检查你的事件日志/跟踪onResume / createView等中的事件。