user1920582 user1920582 -5 years ago 159
Android Question

onActivityResult not called in fragment android

This is my code to take picture from gallery.

public class FragmentLayout1 extends Fragment implements OnClickListener {

View root;
Context c;
Button add_image;
DialogAddImage image;
RelativeLayout layout_image;
String path;
RunAnimations anima;

public void setContext(Context c){
this.c = c;
Constants con = new Constants(c);

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

anima = new RunAnimations();
image = new DialogAddImage((Activity) c);

Bundle bun = new Bundle();
path = bun.getString("path");

root = inflater.inflate(R.layout.layout_1, container, false);
add_image = (Button)root.findViewById(;

layout_image = (RelativeLayout)root.findViewById(;

Log.e("path", path);
Drawable d = Drawable.createFromPath(path);

return root;


public void onClick(View v) {
// TODO Auto-generated method stub
anima.loadAnimationView(c, R.anim.alpha_button, v);
if(v == add_image){


//============= fungsi untuk menerima hasil pilihan user dalam kotak dialog ambil gambar=============
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.e("result", "Result");
new ImageResult((Activity) c).resultOfImage(requestCode, resultCode, data, image.getUri(), false);

in method on click , I've button add_image. add_image will show a dialog for user to take picture from camera or gallery
And this is my dialog code

public class DialogAddImage{
private Activity c;
private Uri mImageCaptureUri;
private Dialog dialog;
AnimasiActivity aa;
Button camera, galeri;

public DialogAddImage(Activity c){
this.c = c;

aa = new AnimasiActivity(c);

//untuk mendapatkan uri yang menyimpan informasi path file image
public Uri getUri(){
return mImageCaptureUri;

private void setDialog(){
dialog = new Dialog(c);
dialog.getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));

camera = (Button) dialog.findViewById(;
galeri = (Button)dialog.findViewById(;

//kalo user pilih dari kamera
camera.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
String file_name = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date());
File file = new File(Constants.path_image +file_name + ".jpg");
mImageCaptureUri = Uri.fromFile(file);

intent.putExtra(MediaStore.EXTRA_OUTPUT, mImageCaptureUri);

try {
intent.putExtra("return-data", true);
intent.putExtra("mImageCaptureUri", mImageCaptureUri);

aa.startForwardForResult(intent, Constants.PICK_FROM_CAMERA);
} catch (Exception e) {


//kalo user pilih dari galery
galeri.setOnClickListener(new OnClickListener(){

public void onClick(View arg0) {
Intent intent = new Intent();

aa.startForwardForResult(intent, Constants.PICK_FROM_FILE);



public void showDialog(){;

public void hideDialog(){


But when I've choose image from gallery , this image not showing in my fragment. And method onActivityResult never called, but why??? any solution , please

Answer Source

Override onActivityResult in parent Activity i.e. parent of all fragment

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download