Lộc Trần Phước
Lộc Trần Phước

Reputation: 319

onActivityResult is deprecated in DialogFragments but not in Activity

in activity this method still work but when i call it in Dialog Fragments, it is deprecated.

in DialogFragment enter image description here

in Activity enter image description here

Edit:i use the Matisse library for load image , it like

library link: https://github.com/zhihu/Matisse

 Matisse.from(getActivity())
                        .choose(MimeType.ofImage())
                        .countable(true)
                        .maxSelectable(9)
                        .addFilter(new GifSizeFilter(320, 320, 5 * Filter.K * Filter.K))
                        .restrictOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)
                        .thumbnailScale(0.85f)
                        .imageEngine(new GlideEngine())
                        .showPreview(false) // Default is `true`
                        .forResult(REQUEST_CODE_AVATAR);

how can i use ActivityResultLaucher for this library?

explain to me why and how can i fix it? please

Have a nice day,everyone!

Upvotes: 2

Views: 911

Answers (1)

Drivers Sea
Drivers Sea

Reputation: 548

First thing is that result activity is deprecated. This is the new way to obtain results from an activity. You should be able to do similarly with a dialog fragment. The reason it was deprecated is because when there is low memory inactive activities are often removed along with the result and this causes errors.

// You can do the assignment inside onAttach or onCreate, i.e, before the activity is displayed
    ActivityResultLauncher<Intent> someActivityResultLauncher = registerForActivityResult(
            new ActivityResultContracts.StartActivityForResult(),
            new ActivityResultCallback<ActivityResult>() {
                @Override
                public void onActivityResult(ActivityResult result) {
                    if (result.getResultCode() == Activity.RESULT_OK) {
                        // There are no request codes
                        Intent data = result.getData();
                        doSomeOperations();
                    }
                }
            });

    public void openSomeActivityForResult() {
        Intent intent = new Intent(this, SomeActivity.class);
        someActivityResultLauncher.launch(intent);
    }

Upvotes: 2

Related Questions