git.iko.soy/opencode-android
on 61cdc75 help sign in
15 lines 624 B history raw
1package soy.iko.opencode.util
3import android.app.Activity
4import android.content.Context
5import android.content.ContextWrapper
7/** Walk the [Context] wrapper chain to find the hosting [Activity], so window/fold/lifecycle
8 * APIs reachable only from an Activity can be used from a Composable via [LocalContext].
9 * Returns null when the context isn't hosted in an Activity (e.g. a preview), in which case
10 * the caller should skip the Activity-dependent path. */
11tailrec fun Context.findActivity(): Activity? = when (this) {
12 is Activity -> this
13 is ContextWrapper -> baseContext.findActivity()
14 else -> null