OnActivityResult not being called in android fragment
This is my code to take a picture from the 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);
}
@Override
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(R.id.button2);
add_image.setOnClickListener(this);
layout_image = (RelativeLayout)root.findViewById(R.id.layout_image);
if(!TextUtils.isEmpty(path)){
Log.e("path", path);
Drawable d = Drawable.createFromPath(path);
layout_image.setBackground(d);
}
return root;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
anima.loadAnimationView(c, R.anim.alpha_button, v);
if(v == add_image){
image.showDialog();
}
}
//============= fungsi untuk menerima hasil pilihan user dalam kotak dialog ambil gambar=============
@Override
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 the method on click, I have an add_image button. add_image will show a dialog for the user to take a picture from the 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);
setDialog();
}
//untuk mendapatkan uri yang menyimpan informasi path file image
public Uri getUri(){
return mImageCaptureUri;
}
@SuppressWarnings("deprecation")
private void setDialog(){
dialog = new Dialog(c);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialog_add_image);
dialog.getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
camera = (Button) dialog.findViewById(R.id.button1);
galeri = (Button)dialog.findViewById(R.id.button2);
//kalo user pilih dari kamera
camera.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
hideDialog();
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) {
e.printStackTrace();
}
}
});
//kalo user pilih dari galery
galeri.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
hideDialog();
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
aa.startForwardForResult(intent, Constants.PICK_FROM_FILE);
}
});
}
public void showDialog(){
dialog.show();
}
public void hideDialog(){
dialog.dismiss();
}
}
But when I select an image from the gallery, this image is not displayed in my fragment. And the onActivityResult method never called, but why ??? any solution please
source to share
Make sure you are calling startActivityForResult () and not getActivity (). startActivityForResult () from your fragment. refer onActivityResult is not called on fragment
source to share
Also, if you call startActivityForResult()
from your fragment, your fragment will be called onActivityResult()
. And if you call startActivityForResult()
from your activity, then your activity will be called onActivityResult()
. Basically where you call startActivityForResult()
it gets called onActivityResult()
.
Another thing, on Android, the preferred way to create dialogs is by extending the DialogFragment class .
source to share
The problem is very sensitive and a little tricky to observe (fix) which I did for this in the following way:
When u is called Activity B from Activity A, the origin activity instance (A) must be on the stack (memory) in order to trigger the onActivityResult callback.
The problem is explained below,
Observation . From material design back and NavUtils.navigateUpFromSameTask - describe navigateUpFromSameTask as follows "A convenience method that is equivalent to calling navigateUpTo (sourceActivity, getParentActivityIntent (sourceActivity)). SourceActivity will be terminated by this call."
remember sourceActivity (A) is removed from the stack (memory) when using this method.
when there is no sourceActivity (A) attribute, there is no underlying instance (A) to invoke the "onActivityResult" callback.
source to share
When you call startActivityForResult
from a fragment, the result is returned to your activity onActivityResult
. Use super.onactivityResult
in your activity instead onActivityResult
and the result will be sent back to your fragment onActivityResult
. You can write your code here. Don't use getActivity.onactivityResult
in your fragment onActivityResult
, because it will refer to an action onActivityResult
.
source to share