They don't (as of Jan 2022) host the @Preview Jetpack Compose code in a your MainActivity when Preview-ing items in Android Studio.
This means you can walk up the context.baseContext tree to determine if you're in your app's MainActivity class or another app's top-level activity class. My top-level activity is called MainActivity, so:
fun Context.findMainActivityOrNull(): MainActivity?{
var context = this
while (context is ContextWrapper) {
if (context is MainActivity) return context
context = context.baseContext
}
return null
}
This makes it so I can avoid setting the status bar color when in preview, that way it doesn't crash the preview trying to do that.
You call the above function like this:
context.findMainActivityOrNull() //when in a non-@Composable area
If you're in a @Composable function, then you should be able to go:
LocalContext.current.findMainActivityOrNull()
You can get fancy and wrap the misbehaving class up in an adapter to be neat and tidy about it, or just check if the result of the above is null when working around a few issues.