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


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!!!!




3 Answers
3



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.


Activity#startActivityForResult()


onActivityResult



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


Fragment#startActivityForResult()


onActivityResult



(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.)


FragmentActivity



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.


startActivityForResult()



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


super





Thank you !!! I don't need to move my request to fragment or response handling to activity and neither pass it to super implementation, instead just pass fragment to IntentHelper()
– Parikshit Chalke
Jun 29 at 12:58



I think you can try this in your activity's onActivityResult, call super.onActivityResult()



and in fragment
in your fragment make sure you call


startActivityForResult(intent,111); instead of getActivity().startActivityForResult(intent,111); inside your fragment.





I don't get it!!!
– Parikshit Chalke
Jun 29 at 11:44



You have to call
super.onActivityResult(requestCode, resultCode, data); in the onActivityResult() of the activity that houses the fragment before the fragment itself can receive onActivityResult() triggers made. Simply put the following in your Activity:


super.onActivityResult(requestCode, resultCode, data);


onActivityResult()


onActivityResult()


@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}





NOT WORKING !!!
– Parikshit Chalke
Jun 29 at 11:47





Pass your fragment to IntentHelper as IntentHepler(MyFragment fragment) then instantiate it like IntentHelper intentHelper = new IntentHelper(this);, if you need Activity instance inside IntentHelper, you may get it using fragment.getActivity(). I think it should work that way.
– Tosin John
Jun 29 at 11:57



IntentHepler(MyFragment fragment)


IntentHelper intentHelper = new IntentHelper(this);


IntentHelper


fragment.getActivity()






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Export result set on Dbeaver to CSV

Opening a url is failing in Swift