The activity hosting this fragment has its onActivityResult
called when the camera activity returns.
My fragment starts an activity for a result with the intent sent for the camera to take a picture. The picture application loads fine, takes a picture, and returns. The onActivityResult
however is never hit. I've set breakpoints, but nothing is triggered. Can a fragment have onActivityResult
? I'd think so since it's a provided function. Why isn't this being triggered?
ImageView myImage = (ImageView)inflatedView.findViewById(R.id.image);
myImage.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, 1888);
}
});
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if( requestCode == 1888 ) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
((ImageView)inflatedView.findViewById(R.id.image)).setImageBitmap(photo);
}
}
If you use Android support library (AppCompat). This doesn't work if the fragments are nested, as only the top fragment will be in the list. I believe that super. onActivityResult() is already calling onActivityResult() on all of the fragments in the list, so this idea is redundant.
activity:activity-ktx to 1.2. 0 . It has deprecated startActivityForResult in favour of registerForActivityResult . It was one of the first fundamentals that any Android developer has learned, and the backbone of Android's way of communicating between two components.
The android startActivityForResult method, requires a result from the second activity (activity to be invoked). In such case, we need to override the onActivityResult method that is invoked automatically when second activity returns result.
The hosting activity overrides onActivityResult()
, but it did not make a call to super.onActivityResult()
for unhandled result codes. Apparently, even though the fragment is the one making the startActivityForResult()
call, the activity gets the first shot at handling the result. This makes sense when you consider the modularity of fragments. Once I implemented super.onActivityResult()
for all unhandled results, the fragment got a shot at handling the result.
And also from @siqing answer:
To get the result in your fragment make sure you call startActivityForResult(intent,111);
instead of getActivity().startActivityForResult(intent,111);
inside your fragment.
I think you called getActivity().startActivityForResult(intent,111);
. You should call startActivityForResult(intent,111);
.
Option 1:
If you're calling startActivityForResult()
from the fragment then you should call startActivityForResult()
, not getActivity().startActivityForResult()
, as it will result in fragment onActivityResult().
If you're not sure where you're calling on startActivityForResult()
and how you will be calling methods.
Option 2:
Since Activity gets the result of onActivityResult()
, you will need to override the activity's onActivityResult()
and call super.onActivityResult()
to propagate to the respective fragment for unhandled results codes or for all.
If above two options do not work, then refer to option 3 as it will definitely work.
Option 3:
An explicit call from fragment to the onActivityResult function is as follows.
In the parent Activity class, override the onActivityResult() method and even override the same in the Fragment class and call as the following code.
In the parent class:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.dualPane);
fragment.onActivityResult(requestCode, resultCode, data);
}
In the child class:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// In fragment class callback
}
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