Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onActivityResult() not called when Activity started from Fragment

I have an issue with importing a picture from the Album in Android, because the onActivityResult() method is never called.

This is the code that I wrote (called from a fragment not an activity):

Intent galleryIntent = new Intent(Intent.ACTION_PICK,  android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); getActivity().startActivityForResult(galleryIntent, PICK_IMAGE); 

And by the way, I have defined the onActivityResult() but it's never triggered:

@Override public void onActivityResult(int requestCode, int resultCode, Intent data) {     Log.d(TAG, "onActivityResult"); // not printed } 

Any idea what's wrong with this?

Thanks!

like image 875
Amokrane Chentir Avatar asked Jan 12 '12 17:01

Amokrane Chentir


People also ask

How can we call fragment onActivityResult from activity?

To get the result in your fragment make sure you call startActivityForResult(intent,111); instead of getActivity(). startActivityForResult(intent,111); inside your fragment. @StErMi Make sure you call startActivityForResult() and not getActivity(). startActivityForResult() from your fragment.

Can we use onActivityResult in fragment Android?

Android Intent Getting a result from Activity to FragmentReceiving the result can be done using the Fragment 's method onActivityResult() . You need to make sure that the Fragment's parent Activity also overrides onActivityResult() and calls it's super implementation.

How can I call onActivityResult of fragment from activity in Android?

Within your fragment, you need to call: startActivityForResult(myIntent, MY_INTENT_REQUEST_CODE); where myIntent is the intent you already defined, and MY_INTENT_REQUEST_CODE is the int constant you defined in this fragment as a global variable as the request code for this intent.

What is the use of onActivityResult in Android?

When you done with the subsequent activity and returns, the system calls your activity's onActivityResult() method. This method includes three arguments: @The request code you passed to startActivityForResult() . @A result code specified by the second activity.


2 Answers

To have onActivityResult() called in the fragment, you should call the fragment's version of startActivityForResult(), not the activity's. So in your fragment's code, replace

getActivity().startActivityForResult(galleryIntent, PICK_IMAGE); 

with

startActivityForResult(galleryIntent, PICK_IMAGE); 
like image 111
gnobal Avatar answered Sep 19 '22 08:09

gnobal


With this code:

Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); getActivity().startActivityForResult(galleryIntent, PICK_IMAGE); 

The onActivityResult must be in the Activity that contains the Fragment. From there you can call any method of the fragment, not in the fragment.

MyFragment myFragment = (MyFragment) getSupportFragmentManager().findFragmentById(R.id.fragment); myFragment .onCameraResult(requestCode, resultCode, intent); 

to do there whatever you want

like image 31
jegumi Avatar answered Sep 19 '22 08:09

jegumi