0
public class Profile extends Fragment implements Profile_frg{


imageView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                final Dialog d = new Dialog(mainActivity);
                d.requestWindowFeature(Window.FEATURE_NO_TITLE);
                d.setContentView(R.layout.activity_custom_dialog);
                d.setCanceledOnTouchOutside(true);


                gallery = (ImageView) d.findViewById(R.id.imageView1);
                camera = (ImageView) d.findViewById(R.id.imageView2);
                cancel = (ImageView) d.findViewById(R.id.imageView3);

                cancel.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        d.dismiss();
                    }
                });


                gallery.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        try {
                            Intent gintent = new Intent();
                            gintent.setType("image/*");
                            gintent.setAction(Intent.ACTION_GET_CONTENT);
                            startActivityForResult(Intent.createChooser(
                                    gintent, "Select Picture"), PICK_IMAGE);
                        } catch (Exception e) {
                            Toast.makeText(mainActivity,
                                    e.getMessage(), Toast.LENGTH_LONG).show();
                            Log.e(e.getClass().getName(), e.getMessage(), e);
                        }

                        d.dismiss();
                    }

                });

                camera.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {

                        // define the file-name to save photo taken by Camera
                        // activity
                        String fileName = "new-photo-name.jpg";
                        // create parameters for Intent with filename
                        ContentValues values = new ContentValues();
                        values.put(MediaStore.Images.Media.TITLE, fileName);
                        values.put(MediaStore.Images.Media.DESCRIPTION,
                                "Image captured by camera");
                        // imageUri is the current activity attribute, define
                        // and save it for later usage (also in
                        // onSaveInstanceState)
                        imageUri = context.getContentResolver().insert(
                                MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                                values);
                        // create new Intent
                        Intent intent = new Intent(
                                MediaStore.ACTION_IMAGE_CAPTURE);
                        intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
                        intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);

                        startActivityForResult(intent, PICK_Camera_IMAGE);

                        d.dismiss();
                    }

                });
                d.show();
            }
        });

}// Work Fine till here...



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

    }//didn't detect this method
Parsania Hardik
  • 4,593
  • 1
  • 33
  • 33

2 Answers2

2

Yes, there is no onActivityResult() callback in fragments. You have to override activityResult method in your host activity(in which your fragment is defined)

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
   if(requestCode == GALLERY/CAMERA_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
       Fragment yourFragment = getSupportFragmentManager().findFragmentByTag("FRAGMENT_TAG"); // same tag while adding fragment for the first time.
       if (yourFragment != null) {
           yourFragment.onActivityResult(requestCode, resultCode, data); //calling method that should be defined in your fragment.
       }
   }
   super.onActivityResult(requestCode, resultCode, data);
}

And in your fragment do like this :

public void onActivityResult(int requestCode,int resultCode,Intent data) {
   ...
   Pull your image data from data object
   do your further process from here.
   ...
}
Ambar Jain
  • 508
  • 5
  • 11
  • thanks... Still not get the solution may be doing something wrong.. Actually in Fragment class I have to write my usual code in activity result ryte? and in main class I have to write – HYFY Movies Jun 01 '16 at 08:37
  • @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if(requestCode == GALLERY/CAMERA_REQUEST_CODE && resultCode == Activity.RESULT_OK) { Fragment yourFragment = getSupportFragmentManager().findFragmentByTag("FRAGMENT_TAG"); // same tag while adding fragment for the first time. if (yourFragment != null) { yourFragment.onActivityResult(requestCode, resultCode, data); //calling method that should be defined in your fragment. } } super.onActivityResult(requestCode, resultCode, data); } – HYFY Movies Jun 01 '16 at 08:37
  • Right. Are you still facing same problem ? – Ambar Jain Jun 01 '16 at 08:49
  • Yes! I'm facing same problem :( – HYFY Movies Jun 01 '16 at 08:54
  • Actually I'm using fragment with view pager..! – HYFY Movies Jun 01 '16 at 08:55
  • @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {if(requestCode == PICK_IMAGE/PICK_Camera_IMAGE && resultCode == Activity.RESULT_OK) { Profile yourFragment = (Profile) getFragmentManager().findFragmentByTag(Profile.FRAGMENT_TAG); // same tag while adding fragment for the first time. if (yourFragment != null) { yourFragment.onActivityResult(requestCode, resultCode, data); //calling method that should be defined in your fragment. } } super.onActivityResult(requestCode, resultCode, data); } – HYFY Movies Jun 01 '16 at 11:40
  • Can you please share your host activity code and viewpager adapter code so that I will try to solve your problem. – Ambar Jain Jun 03 '16 at 00:51
0

Yes, startActivityForResult will not work directly if you are calling it from any fragment. After getting the result, the callback will hit the onActivityResult of the Hosting Activity from where you have to manually redirect it to the respective fragment.

Below is the sample code of the onActivityResult of your Activity. Please note that this only redirect the result to the respective fragment.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
        case 1001:
            Fragment frag = getSupportFragmentManager().findFragmentByTag("TAG"); // TAG should be same as the one you entered while adding the fragment
            if (frag != null) {
                frag .onActivityResult(requestCode, resultCode, data);
            }
            break;
    }
    super.onActivityResult(requestCode, resultCode, data);
}
Yasir Tahir
  • 790
  • 1
  • 11
  • 31