6
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Android Studio DolphinでActivity#registerForActivityResultを認識してくれない問題の解決方法

Last updated at Posted at 2022-10-26

概要

Android Studio Dolphin | 2021.3.1 Patch 1でも(Chipmunkの時から)以下のようになる問題のワークアランドです。これはAndroid Studioのバグです。

スクリーンショット 2022-10-26 22.52.57.png

解決方法

(this as ComponentActivity)とキャストすれば良いです。

    private val launcher =
        (this as ComponentActivity).registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted: Boolean ->

        }

Android Studioが直ったら面倒なので、以下のExtensionを用意して対応してます。

fun <I, O> Activity.registerForActivityResult(
    contract: ActivityResultContract<I, O>,
    callback: ActivityResultCallback<O>
) = (this as ComponentActivity).registerForActivityResult(contract, callback)
6
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?