0

My problem is want to send interface instance to camera activity so that in onActivityResult I can call method of that interface implementation. I tried following logic that make custom interface made serializable but got error.

I just want to know proper way to make custom interface serializable, I used below code

public interface ImageListener extends Serializable{
    public void onImageCaptureResult(int requestCode, int resultCode, Intent data);
}

Implementation is in my fragment

ImageListener imageListener = new ImageListener() {
    @Override
    public void onImageCaptureResult(int requestCode, int resultCode, Intent data) {
        displayToast("image got in fragment ");
    }
};

but when I put instance of interface in intent.putExtra like this

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra("il",imageListener); // ERROR AT THIS LINE
getActivity().startActivityForResult(intent, ConstantsTags.REQUEST_CAMERA);

I got Exception java.io.NotSerializableException.

Please guide me how can I achieve this.

Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
Khizar Hayat
  • 3,427
  • 3
  • 18
  • 22
  • Why do you even need to pass your interface here. `onActivityResult` will be called with same request code. When you hit that part call your interface method like `imageListener.onImageCaptureResult(...)` – Rohit5k2 Mar 14 '16 at 12:34
  • because imageListener is implemented in my fragment and onActivityResult call back fall in activity. – Khizar Hayat Mar 14 '16 at 12:36
  • Call your Fragment's `onActivityResult` then. See this how to do that http://stackoverflow.com/questions/6147884/onactivityresult-not-being-called-in-fragment – Rohit5k2 Mar 14 '16 at 12:38
  • I have followed those steps but in onActivityResult the request code is changed when i called startActivityForResult in fragment . but when i use getActivity().startActivityForResult(); then i got right request code. But with super call i didn't receive call back in fragment's onActivityResult – Khizar Hayat Mar 14 '16 at 13:24
  • 1
    Thanks Rohit i got solution. i called with getActivity().startActivity and in onActivityResult call fragments custom method – Khizar Hayat Mar 14 '16 at 13:32

0 Answers0