0

I have a problem regarding onActivityResult. First, I opened the Add New Message Fragment to select the image. When I select the image, it doesn't return the result to my current fragment. I put onActivityResult in both my MainActivity and AddMessageFragment but it doesn't call the result in my fragment. My MainActivity is used to set up the Navigation Controller. I use Matisse library for my image picker. Can someone please help me with me? Been stuck for the whole day with this issue.

MainActivity

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

AddMessageFragment

@OnClick({R.id.addImage})
public void openGallery()
{
    permission.addOnStorageListener(new Permission.OnStorageSuccess()
    {
        @Override
        public void OnStorageSuccess()
        {
            permission.requestCamera();
        }
    });
    permission.addOnCameraListener(new Permission.OnCameraSuccess()
    {
        @Override
        public void OnCameraSuccess()
        {
            Matisse.from(getActivity())
                    .choose(MimeType.of(MimeType.JPEG, MimeType.PNG))
                    .countable(false)
                    .capture(true)
                    .captureStrategy(new CaptureStrategy(true, "com.gprop.users.fileprovider"))
                    .maxSelectable(1)
                    .restrictOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)
                    .thumbnailScale(0.85f)
                    .imageEngine(new Glide4Engine())
                    .originalEnable(false)
                    .forResult(99);
        }
    });
    permission.requestStorage();
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == 99 && resultCode == Main.RESULT_OK)
    {
            mSelected = Matisse.obtainPathResult(data);

            if(mSelected.size() > 0)
            {
                Glide.with(this).load(mSelected.get(0)).into(addImage);

                Luban.compress(new File(mSelected.get(0)), getActivity().getFilesDir())
                        .putGear(Luban.THIRD_GEAR)
                        .asObservable()
                        .subscribe(new Consumer<File>()
                        {
                            @Override
                            public void accept(File file) throws Exception
                            {
                                newfile = file;
                            }
                        }, new Consumer<Throwable>()
                        {
                            @Override
                            public void accept(Throwable throwable) throws Exception
                            {
                                throwable.printStackTrace();
                                Toast.makeText(getActivity(), throwable.getMessage(), Toast.LENGTH_SHORT).show();
                            }
                        });
            }
            else
            {
                Toast.makeText(getActivity(), "No image selected", Toast.LENGTH_SHORT).show();
            }

        }
}

enter image description here

Haziq Abdullah
  • 83
  • 1
  • 10

3 Answers3

5

You are calling Matisse.from() on the Activity which you are getting from the getActivity() method, tit must be returning you your MainActivity and the onActivityResult will be called for the MainActivity where you are doing nothing and calling super.onActivityResult().

Possible Solutions: .

  1. Shift your onActivityResult code logic from your AddMessageFragment to MainActivity
  2. Change you Matisse call from Matisse.from(getActivity()) to Matisse.from(this), because looking at the source code of Matisse it also supports Fragment context.

Using the second solution you should get your onActivityResult callback in the fragment and you won't need to change/shift any other code logic.

Hope this helps :)

Yugansh Tyagi
  • 646
  • 10
  • 24
0

Replace your activity onActivityResult method by below code

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);
    Fragment fragment = (Fragment) getChildFragmentManager().findFragmentByTag(childTag);
    if (fragment != null) {
        fragment.onActivityResult(requestCode, resultCode, intent);
    }
}

It will call your fragment onActivityResult after this execute.

Akshay Soni
  • 109
  • 5
0

OnActivityResult() function works on Activity. I suggest you call this function on your activity and implement your logic inside your fragment onResume() function. In order to get data from Activity use global val.

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Vikram Parimi Sep 16 '22 at 11:56