onActivityResult不能在Fragment中调用

该应用程序的结构是这样的:

tabHost (in Activity) -> contains -> TabFragment(extend base container fragment) 

1.活动中的代码

 tabHost.addTab( tabHost.newTabSpec("home").setIndicator("", getResources().getDrawable(R.drawable.btn_home)), HomeFragment.class, null); 

2. HomeFragment中的代码 (注意,HomeFragment不是实际的函数,而是一个像这样的容器,它扩展了BaseContainerFragment):

 public class HomeFragment extends BaseContainerFragment { public Home homeFrag; private boolean mIsViewInited; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.container_fragment, null); } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); if (!mIsViewInited) { mIsViewInited = true; initView(); } } private void initView() { homeFrag = new Home(); replaceFragment(homeFrag, false); } } 

3. BaseContainerFragment

 public class BaseContainerFragment extends Fragment { public void replaceFragment(Fragment fragment, boolean addToBackStack) { FragmentTransaction transaction = getChildFragmentManager().beginTransaction(); if (addToBackStack) { transaction.addToBackStack(null); } transaction.replace(R.id.container_framelayout, fragment); transaction.commit(); } public boolean popFragment() { boolean isPop = false; if (getChildFragmentManager().getBackStackEntryCount() > 0) { isPop = true; getChildFragmentManager().popBackStack(); } return isPop; } } 

4.在Home(片段的实际内容)

 UploadType fragment = new UploadType(); Bundle bundle = new Bundle(); bundle.putString("form_type", "request"); fragment.setArguments(bundle); ((BaseContainerFragment)getParentFragment()).replaceFragment(fragment, true); 

5.在UploadType中,我调用相机活动,但是ActivityResult仅在主活动中返回。

 startActivityForResult(intent, REQUEST_CAMERA); public void onActivityResult(int requestCode, int resultCode, Intent data) { Log.d("test1", "result2"); super.onActivityResult(requestCode, resultCode, data); } 

我怎样才能触发UploadType的onActivityResult? 感谢帮助。

Solutions Collecting From Web of "onActivityResult不能在Fragment中调用"

这不起作用的原因是因为您从嵌套的片段内调用startActivityForResult() 。 Android足够聪明,可以将结果传回给一个Activity甚至一个Fragment ,而不是嵌套的Fragment因此你不会得到callback。 (更多的信息为什么这不起作用或在计算器上 )

现在为了使它工作,我build议你手动将callback路由到ChildFragment (= UploadType )中的ParentFragment (= BaseContainerFragment ):

 protected void onActivityResult(int requestCode, int resultCode, Intent data) { Fragment uploadType = getChildFragmentManager().findFragmentById(R.id.container_framelayout); if (uploadType != null) { uploadType.onActivityResult(requestCode, resultCode, data); } super.onActivityResult(requestCode, resultCode, data); } 

在我的情况下,我已经通过在我的MainActivity.java添加下面的代码

 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); for (Fragment fragment : getSupportFragmentManager().getFragments()) { fragment.onActivityResult(requestCode, resultCode, data); } } 

在你的活动覆盖onActivityForResult()像这样

 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); } 

现在在你的片段中,你可以得到这个里面的活动结果

 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { Log.d("test1", "result2"); } 

确保当你正在调用启动ActivityForResult在你的碎片应该是这样的

 startActivityForResult(intent, REQUEST_CAMERA); 

TabActivity-> ActivityA-> FragmentB,这是行不通的。

使用坏坏坏的方式:

ActivityA.java

 public void onSelectSomething(){ ... startActivityForResult(intent, 22222); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (mFragment != null) { mFragment.onActivityResult(requestCode, resultCode, data); } } 

FragmentB.java

 if(getActivity() instanceof ActivityA) { ((RepairerListActivity)getActivity()).onSelectSomething(); } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == 22222) { // do things } }