androidstartactivityforresult

call to startActivityForResult() from non activity class and getting result in existing activity or fragment


My IntentHelper class is

public class IntentHelper {

    private Activity activity;

    // Camera activity request code
    public static final int CAMERA_CAPTURE_IMAGE_REQUEST_CODE = 100;
    //Gallery activity request code
    public static final int GALLERY_OPEN_IMAGE_REQUEST_CODE = 101;

    public IntentHelper(Activity activity){
        this.activity = activity;
    }

    /**
     * Launching gallery to get image
     */
    public void getGalleryImage(){

        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        intent.putExtra("scale", true);
        intent.putExtra("outputFormat",
                Bitmap.CompressFormat.JPEG.toString());

        activity.startActivityForResult(Intent.createChooser(intent, "Select Image"),GALLERY_OPEN_IMAGE_REQUEST_CODE);
    }
}

I am calling this class from fragment as :

IntentHelper intentHelper = new IntentHelper(getActivity());
intentHelper.getGalleryImage();

My gallery intent is successfully open with this logic. But :

 @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    // following if condition is not executing
    if(requestCode == IntentHelper.GALLERY_OPEN_IMAGE_REQUEST_CODE)
    {
        if(resultCode == RESULT_OK){

            SnackbarHelper.showSnackBar("Image browsed sucessfully",getView());
            Toast.makeText(getContext(), "Image browsed successfully", Toast.LENGTH_SHORT).show();

        }
        else if(requestCode == RESULT_CANCELED){
            SnackbarHelper.showSnackBar("Image browsed cancelled",getView());
        }
    }
}

I don't know why is this happening. My intent is called perfectly and is returning me to same activity then why this onActivityResult() condition is not working ???

My all code is in fragment. But I don't think so it cause this problem

EDIT - SOLUTION

As answer given by @laalto is true. I have to start startActivityForResult() with fragment instance.

Hence I changed my IntentHelper class as :

public class IntentHelper {
/*
* Instead of Activity use Fragment
*/
private Fragment fragment;

// Camera activity request code
public static final int CAMERA_CAPTURE_IMAGE_REQUEST_CODE = 100;
//Gallery activity request code
public static final int GALLERY_OPEN_IMAGE_REQUEST_CODE = 101;

public IntentHelper(Fragment fragment){
    this.fragment = fragment;
}

/**
 * Launching gallery to get image
 */
public void getGalleryImage()  {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    intent.putExtra("aspectX", 1);
    intent.putExtra("aspectY", 1);
    intent.putExtra("scale", true);
    intent.putExtra("outputFormat",
            Bitmap.CompressFormat.JPEG.toString());

    // Use fragment for startActivityForResult() to initiate.
    fragment.startActivityForResult(Intent.createChooser(intent, "Select Image"),GALLERY_OPEN_IMAGE_REQUEST_CODE);
}

}

IntentHelper is initiated as :

IntentHelper intentHelper = new IntentHelper(this);
intentHelper.getGalleryImage();

After this changes everything works fine!!!!


Solution

  • My all code is in fragment. But I don't think so it cause this problem

    It's the problem.

    If you call Activity#startActivityForResult(), you receive onActivityResult in activity.

    If you call Fragment#startActivityForResult(), you receive onActivityResult in that fragment.

    (How this is done under the hood is that FragmentActivity encodes a fragment index to the request code for those calls where the result ought to go to a fragment.)

    So, calling startActivityForResult() in an activity and expecting to see the result in a fragment does not work. Either move the request to fragment, or the response handling to activity.

    Also, if you don't handle a result, pass it on to the super implementation.