Jetpack Compose で Activity を取得する

なんとなくまだ古くさい感じはありますが。


val context = LocalContext.current


fun Context.getActivityOrNull(): Activity? {
    var context = this
    while (context is ContextWrapper) {
        if (context is Activity) return context
        context = context.baseContext
    }    
    return null
}


fun Context.findActivity(): Activity {
    var context = this
    while (context is ContextWrapper) {
        if (context is Activity) return context
        context = context.baseContext
    }
    throw IllegalStateException("Permissions should be called in the context of an Activity")
}

👉 android - How to get activity in compose - Stack Overflow


関連ワード:  AndroidAndroidStudioJetpackComposeKotlin今さら聞けない便利な設定初心者開発