0

I have searched for solutions about my problem but didn't find something that match exactly my project configuration. So let's me explain you.

I have a MainActivity that contains NavigationView. The MainActivity contains a MainActivityFragment - via NavigationView, I wish you understand exactly what I want to say. It's the default and the only one Fragment accessible directly from MainActivity.

Here's how I show MainActivityFragment in MainActivity:

FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content, new MainActivityFragment()).commit();

When clicking on a Navigation Menu Item, it will show a DialogFragment:

if (id == R.id.nav_add_poi) {
    new AddPoiFragment().show(getSupportFragmentManager(), AddPoiFragment.TAG);
} else if (id == R.id.nav_report_event) {
    new AddEventFragment().show(getSupportFragmentManager(), AddEventFragment.TAG);
}

The MainActivityFragment contains a ViewPager with four fragments.

In AddPoiFragment (a DialogFragment), the user can take a photo. Here's how I manage the onActivityResult stuff:

startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {
        Toast.makeText(getActivity(), "Hello", Toast.LENGTH_SHORT).show();
    }
}

It seems that onActivityResult is never triggered. I have tried with getActivity().startActivityForResult() following some other solutions but as I have said, these solutions don't match exactly my configuration and I get errors when using them.

tsil
  • 2,069
  • 7
  • 29
  • 43
  • Doesn't `onActivityResult()` belong to the Activity? – Phantômaxx Oct 04 '16 at 19:08
  • it sure does! check the SDK and the Android developer guides to start an activity for a result. – apelsoczi Oct 04 '16 at 19:12
  • @Rotwang: Argh. The link won't link properly. Anyway, `onActivityResult` is in both Activity and Fragment. – DeeV Oct 04 '16 at 19:20
  • Anyway, the answer is "wherever you need it." Multiple Fragments in one Activity may need the result as well. The Activity itself may need the answer. Be sure to call `super.onActivityResult` in each call. – DeeV Oct 04 '16 at 19:23
  • This answer has the reasons why `onActivityResult` wouldn't get called in a Fragment http://stackoverflow.com/questions/6147884/onactivityresult-is-not-being-called-in-fragment – DeeV Oct 04 '16 at 19:24
  • It's different from my case – tsil Oct 04 '16 at 19:32
  • Isn't it the Activity (`getActivity().startActivityForResult()`) which starts the other Activity for Result? Therefore, I guess that `onActivityResult()` will fire in the calling Activity. – Phantômaxx Oct 04 '16 at 19:48
  • Yes but how to fire the Fragment ``onActivityResult()`` in the Activity? – tsil Oct 04 '16 at 19:51
  • `??` Just get it inside the calling Activity. The Fragment is only the one who tells the Activity1 to start Activity2. And Activity2 returns the result in... Activity1. The Fragment has no longer any role in this. Moreover, the Fragment itself is part of Activity1. – Phantômaxx Oct 04 '16 at 20:26
  • Now I'm calling ``getActivity().onActivityResult()`` in my fragment (AddPoiFragment) and overriding ``onActivityResult()`` in MainActivity. ``onActivityResult()`` is fired but ``Intent data`` is null. – tsil Oct 05 '16 at 18:12
  • This depends on how you are returning the data. And how the REQUEST_TAKE_PHOTO is defined. – Phantômaxx Oct 07 '16 at 07:10

0 Answers0