标签: activity-result-api

如何为 ActivityResultLauncher 构造 PickVisualMediaRequest

我正在尝试使用Activity Result API来处理为我正在开发的应用程序选择单张照片的情况。我正在尝试使用预定义的合同之一来使事情变得简单。因此,我尝试使用ActivityResultContracts.PickVisualMedia()合约。

我将活动结果启动器设置如下:

private ActivityResultLauncher<PickVisualMediaRequest> pickVisualMediaActivityResultLauncher;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    pickVisualMediaActivityResultLauncher = registerForActivityResult(
            new ActivityResultContracts.PickVisualMedia(),
            this::onPickVisualMediaActivityResult
    );
}
Run Code Online (Sandbox Code Playgroud)

我正在尝试构建PickVisualMediaRequest并在此处启动 Activity Result Launcher:

private void onSelectNewPhotoButtonClick() {
    PickVisualMediaRequest request = new PickVisualMediaRequest.Builder()
            .setMediaType(new ActivityResultContracts.PickVisualMedia.ImageOnly())
            .build();
    pickVisualMediaActivityResultLauncher.launch(request);
}
Run Code Online (Sandbox Code Playgroud)

问题是 Android Studio 抱怨 ActivityResultContracts.PickVisualMedia.ImageOnly() 没有适当的可见性来使用,即使它是有效的 VisualMediaType 并且文档暗示它应该这样使用: 在此输入图像描述

我真的找不到关于这个特定场景的任何代码示例。我错过了什么吗?API 是否存在可见性缺陷,或者我今天只是愚蠢?

java android kotlin activity-result-api activityresultcontracts

8
推荐指数
1
解决办法
6943
查看次数

ActivityResultContract 中获取请求代码的默认方式

有没有默认的方式来获取请求代码ActivityResultContract

我知道StartActivityForResult合同,它有回报ActivityResult,但没有requestCode,只有resultCode

我可以做这样的事情,但也许有更好的解决方案:

class StartActivityForResult : ActivityResultContract<ActivityInput, ActivityOutput?>() {
    override fun createIntent(
        context: Context,
        input: ActivityInput
    ): Intent {
        return input.data.apply { putExtra(requestCodeKey, input.requestCode) }
    }

    override fun parseResult(resultCode: Int, intent: Intent?): ActivityOutput? {
        return if (intent == null || resultCode != Activity.RESULT_OK) null
        else ActivityOutput(
            // should never return default value
            requestCode = intent.getIntExtra(requestCodeKey, -1),
            resultCode = resultCode,
            data = intent
        )
    }

    override fun getSynchronousResult(
        context: Context,
        input: …
Run Code Online (Sandbox Code Playgroud)

android android-intent registerforactivityresult activity-result-api

7
推荐指数
0
解决办法
388
查看次数

如何将 registerForActivityResult 与 StartIntentSenderForResult 合约结合使用?

我正在编写一个 Kotlin 应用程序并使用 Firebase 进行身份验证。由于onActivityResult现已废弃,我正在尝试将我的应用程序迁移到使用registerForActivityResult. 我有一个指向 Google 帐户功能的链接,该链接从 Google 登录流程开始,如此处所示。我的代码:

    private fun initGoogleSignInClient() =
        activity?.let {

            // Configure Google Sign In
            val gso =
                GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                    .requestIdToken(getString(R.string.default_web_client_id))
                    .requestEmail()
                    .build()

            // Build a GoogleSignInClient with the options specified by gso.
            viewModel.googleSignInClient = GoogleSignIn.getClient(it, gso)
        }

    private fun showLinkWithGoogle() =
        startActivityForResult(viewModel.googleSignInClient.signInIntent, RC_LINK_GOOGLE)
Run Code Online (Sandbox Code Playgroud)

whereinitGoogleSignInClient在片段的 中调用onCreateView,并showLinkWithGoogle在用户点击屏幕上的按钮时调用。这很有效。我使用 查找了一个示例registerForActivityResult,我发现的最好的示例位于本页底部。我添加了这段代码:

    private val linkWithGoogle =
        registerForActivityResult(ActivityResultContracts.StartIntentSenderForResult()) {
            viewModel.handleGoogleResult(it.data)
        }

    private fun showLinkWithGoogle() = …
Run Code Online (Sandbox Code Playgroud)

android-intent kotlin firebase-authentication google-signin activity-result-api

7
推荐指数
1
解决办法
5798
查看次数

How to get requestCode from Activity Result API

I am very new to Android Kotlin programming. I am making an app to create, read and delete file using Storage Access Framework. I did it by implementing deprecated startActivityForResult intent to perform file read, create, delete tasks. I tried to implement it using Activity Result API but I got stuck in request code which I could not find it. In new implementation, there is resultCode but no requestCode.

My implemetation is below. How I can I implement the following …

android-intent kotlin android-studio activity-result-api

3
推荐指数
1
解决办法
744
查看次数