【问题标题】:onActivityResult is deprecated in DialogFragments but not in ActivityonActivityResult 在 DialogFragments 中已弃用,但在 Activity 中不推荐使用
【发布时间】:2021-08-09 02:04:48
【问题描述】:

在活动中此方法仍然有效,但当我在对话框片段中调用它时,它已被弃用。

在 DialogFragment 中

在活动中

编辑:我使用 Matisse 库来加载图像,就像

库链接: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);

我如何在这个库中使用 ActivityResultLaucher?

向我解释为什么以及如何解决它?请

祝大家有美好的一天!

【问题讨论】:

标签: android deprecated


【解决方案1】:

首先,不推荐使用结果活动。这是从活动中获取结果的新方法。您应该能够对对话框片段进行类似的操作。不推荐使用它的原因是,当内存不足时,不活动的活动通常会随着结果一起被删除,这会导致错误。

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

【讨论】:

  • 我只是编辑了我的问题,在这种情况下你能帮我吗,非常感谢!
猜你喜欢
  • 2021-04-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-17
  • 1970-01-01
  • 2013-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多