简体   繁体   中英

Issue with Fragment it doesn't support Activity Result

//As per some suggestions I write this code in my project but still going to face same issue

@Override

    public void onActivityResult(int requestCode, int resultCode, Intent data) 
    {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);

        if(requestCode == PICK_IMAGE/PICK_Camera_IMAGE && resultCode == Activity.RESULT_OK)
        {
               Fragment yourFragment = getSupportFragmentManager().findFragmentById(R.id.frame_container); // 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.

         }
         }
    }

//this code is from MainActivity.java class.. //Should I write Above Activity Result Code into my Mainfragment class?

 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 = getActivity().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);
        }
        });

// This is the code belongs to my Profile Fragment class..
// I stuck over here didn't get any kind of solution 
// I'm using fragment viewpager 

Here you done mistake if do like this **PICK_IMAGE/PICK_Camera_IMAGE** it will divide your request codes and resulting code will be different

 if(requestCode == PICK_IMAGE/PICK_Camera_IMAGE && resultCode ==Activity.RESULT_OK)
  {
     //...
  }

Use this code instead of older

if(requestCode == PICK_IMAGE && resultCode == Activity.RESULT_OK)
 {
         //...
}else if(requestCode == PICK_Camera_IMAGE && resultCode ==Activity.RESULT_OK)
{
         //...
}

To get the result in your fragment make sure you call

startActivityForResult(intent, PICK_Camera_IMAGE);

instead of

getActivity().startActivityForResult(intent, PICK_Camera_IMAGE); 

inside your fragment.

As all fragments are invoked into an activity so the result will have to be transferred into the fragments from onActivityResult of your activity just like you are doing.

You will have to create you own onActivityResult method in your fragment with same signature as well to update your fragment UI.

删除这个

super.onActivityResult(requestCode, resultCode, data);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM