onActivityResult not call in the Fragment

匿名 (未验证) 提交于 2019-12-03 02:56:01

问题:

The structure of the app is like this:

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

1. The code in Activity:

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

2. The code in HomeFragment (Notice that HomeFragment is not the actual function but a container like this, and it extend 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. In the Home (The actual content of the fragment)

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

5. And in the UploadType , I call the camera activity but onActivityResult is only return in the main activity.

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

How can I trigger the onActivityResult at UploadType? Thanks for help.

回答1:

The reason why this doesn't work is because you are calling startActivityForResult() from within a nested fragment. Android is smart enough to route the result back to an Activity and even a Fragment, but not to a nested Fragment hence why you don't get the callback. (more information to why that doesn't work here or on stackoverflow)

Now in order to make it work I suggest you manually route the callback to the ChildFragment (=UploadType) in the 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); }


回答2:

In my case, I've done by adding following code in my 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);     } }


回答3:

In your Activity Override onActivityForResult() like this

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

Now in your fragment u can get the activity result inside this

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

Make sure when your are calling start ActivityForResult in your frragment it should be like this

startActivityForResult(intent, REQUEST_CAMERA);


回答4:

TabActivity->ActivityA->FragmentB, it's not work.

use a bad bad bad way:

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     } }   


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!