onActivityResult不能使用FragmentTransaction在Fragment中工作

时间:2014-11-02 05:11:07

标签: android

我有FragmentTransaction的问题。我的项目使用FragmentTabhost,其中一个标签是Profile选项卡。 选择配置文件选项卡时,我加载配置文件片段然后我按下编辑按钮,替换为EditProfile。 Profile.java中的代码

public class Profile extends Fragment implements OnClickListener{
........
     public void onClick(View v) {
         EditProfile profile = new EditProfile(); 
         Bundle bundle=new Bundle();
         bundle.putString("Token", tokenId);
         FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
         transaction.replace(R.id.container_framelayout, fragment);
         transaction.commit();
    }
}

代码EditProfile.java

public class Editprofile extends Fragment implements OnClickListener{
.........
     public void onClick(View v) {
          Intent intent = new Intent(
                Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
          intent.setType("image/*");
          startActivityForResult(intent,SELECT_PICTURE);
     }

     @Override
     public void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub

        if(requestCode==SELECT_PICTURE && data!=null)
        {
            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };
            Cursor cursor = getActivity().getContentResolver().query(selectedImage,filePathColumn, null, null, null);
            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);
            cursor.close();

            avatar.setImageBitmap(BitmapFactory.decodeFile(picturePath));
        }
    }
}

在EditProfile Fragment中,我有一个按钮和一个ImageView头像。当按下按钮时,我想获得在ImageView上显示的garlary图片。这个问题是当我从Profile选项卡加载EditProfile时选择它运行良好,但我从Profile Fragment onActivityResult转移不运行。

你能帮助我吗?

2 个答案:

答案 0 :(得分:0)

是的,这是一个已知问题,

从嵌套片段(特别是使用制表符)调用startActivityForResult()时,只调用父活动调用onActivity结果而不是片段中的onActivityResult

请查看以下代码项目,以获取简单的解决方案

https://github.com/samjerusalem/NestedFragment

答案 1 :(得分:0)

似乎此问题已通过Android支持库23.2 +修复。

更多详情:Info1Info2

简单的解决方案是使用Android支持库版本 23.2.1 或更高版本。