2014-11-02 50 views
0

我有FragmentTransaction的问题。我的项目使用FragmentTabhost,其中一个选项卡是Profile选项卡。 当配置文件选项卡是选择我加载配置文件片段。然后按Edit按钮,替换为EditProfile。 代码在Profile.javaonActivityResult不能在使用FragmentTransaction的片段中工作

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片段,我有一个按钮,一个ImageView的化身。当按下按钮时,我想要在ImageView中显示garlary的图片。这个问题是当我从配置文件选项卡加载EditProfile选择它运行良好,但我从配置文件片段onActivityResult转移不运行。

你能帮我吗?

+0

onActivityResult在其中调用片段的主要活动将收回数据..从那里您必须将结果传递到所需的片段 – Meenal 2014-11-02 05:16:20

回答

0

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

虽然从嵌套片段(特别是使用标签)仅父活动调用startActivityForResult()获取onActivity导致片段被调用,而不是onActivityResult

请检查以下代码项目,一个简单的解决方案

https://github.com/samjerusalem/NestedFragment

0

看来这个问题已修复了Android支持库23.2+。

更多细节:Info1Info2

简单的解决方法是使用Android的支持库修订23.2.1以上。