3

When I call :

private void openGallery() {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    getActivity().startActivityForResult(Intent.createChooser(intent, "Select Picture"),
            PICK_IMAGE);
}

not execute never this :

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

And I have also implemented this :

//  @Override
//  public void startActivityForResult(Intent intent, int requestCode) {
//      // TODO Auto-generated method stub
//      super.startActivityForResult(intent, requestCode);
//      
//  }

but still not working.

I have this structure ActionBarAcitivy - > FragmentPagerAdapter -> Fragment - > Fragment - > here startActivityForResult "Fragment"

Note this code extends "Fragment"

THIS POST NOT RESOLVE !

Note after experience :

Not exist any form, I spend 3 days with this issue, and -1 fragments, I put all in a activity (actionbaractivity)

I will not bother to use many fragments, What a programmer can't do is waste time discovering imaginary code.

  • Did you override `onActivityResult()` in the activity too? That could be interfering with this, unless you call `super.onActivityResult()`. – matiash Jun 20 '14 at 19:39
  • I've seen this issue with Child Fragments, is that, by chance, what this fragment is? – jacobhyphenated Jun 20 '14 at 19:40
  • Yes I have @override + super.onActivityResult, and not work –  Jun 20 '14 at 19:44
  • check the onActivityResult() of the fragment activity so you can debug...maybe it's going there being consumed...or if it's a dynamic fragment (not declared in the XML) it might be that the fragment isn't attached to the activity anymore when it returns...not sure, the lifecycle of this fragment has to be checked. – Alécio Carvalho Jun 20 '14 at 22:04
  • fragment activity ??????? I never talk about activity, I have "Fragment" extends "Fragment" and need call onActivityResult() –  Jun 20 '14 at 22:09
  • I have this structure ActionBarAcitivy - > FragmentPagerAdapter -> Fragment - > Fragment - > here "Fragment" I try override ActionBarAcitivy with onActivityResult but not work –  Jun 21 '14 at 17:41

2 Answers2

1

This line is the problem:

getActivity().startActivityForResult(Intent.createChooser(intent, "Select Picture"),
        PICK_IMAGE);

Instead, you should call this method on the fragment, not on the activity. This allows the system to route the response back to the correct fragment. So simply replace it with this:

startActivityForResult(Intent.createChooser(intent, "Select Picture"),
        PICK_IMAGE);

Link to docs for reference: http://developer.android.com/reference/android/support/v4/app/FragmentActivity.html#startActivityFromFragment

Alex MDC
  • 2,416
  • 1
  • 19
  • 17
  • @user3718720 have u added permission? – Asthme Jun 21 '14 at 18:01
  • What permissions I need ? in my other "activity" Works well but in my "fragment" not work –  Jun 21 '14 at 18:03
  • Not exist any form, I spend 3 days with this issue, and -1 fragments, I put all in a activity (actionbaractivity) –  Jun 21 '14 at 21:45
1

There are few key concepts to remember:

  • nested fragments can't handle the onActivityResult, only first level fragments can (ie the fragments you directly add to activity), details here
  • you must use fragment.startActivityForResult to get back results in your onActivityResult. If you do activity.startActivityForResult you will never get the results in your fragment
  • in your onActivityResult methods (all) always call super.onActivityResult
Community
  • 1
  • 1
Marco C.
  • 1,282
  • 2
  • 15
  • 19