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!
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.
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.
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.
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.
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);
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With