简体   繁体   中英

onActivityResult from Activity to Fragment

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

        }
}

在此处输入图像描述

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 :)

An explicit call from fragment to the onActivityResult function is as follows.

In the parent Activity class, override the onActivityResult() method and even override the same in the Fragment Class and call as the following code.

In MainActivity class:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.layoutContainer);
    fragment.onActivityResult(requestCode, resultCode, data);
}

In AddMessageFragment class:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // In fragment class callback
}

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.

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.

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