Hello, is there a way to detect whether a composab...
# compose-android
l
Hello, is there a way to detect whether a composable is run…?: • in interactive preview within Android Studio / IDE • in static preview within Android Studio / IDE • deployed as an interactive preview on a phone • just in an app "normally"
e
not sure why you'd want to, but •
LocalInspectionMode.current
&&
LaunchedEffect { ... }
runs •
LocalInspectionMode.current
&&
LaunchedEffect { ... }
doesn't run • never tried but maybe
LocalContent.current.packageName.endsWith(".test")
👍🏼 1
l
Problem with
LaunchedEffect
is the first composition is off
It's fine in IDE preview, but it's not fine in production
s
DisposableEffect should happen on the first composition
y
The interactive preview on a device is false for
LocalInspectionMode.current
This is an example for switching on interactive/static previews in IDE https://github.com/google/horologist/blob/91b797b563557580968bcc67d8550d1518c8aea0[…]gle/android/horologist/compose/tools/InteractivePreviewAware.kt it has the same first frame, and then once a LaunchedEffect runs, knows it's interactive.
👍 1
l
You're all right, my concern for initial composition in production isn't valid since LocalInspectionMode.current will always tell me it's not in the IDE
l
LaunchedEffect
is actually run for a short amount of time for static preview, so it fails me 🥹
My sad workaround is to add
delay(16)
. I tried calling
yield()
a few times, up to 60 times, and it never worked.
delay(1)
isn't either, but
delay(16)
is.
delay(16)
isn't always working,
delay(33)
seems to work more consistently. I wish I didn't have to do this kind of hack with magic numbers. I'm interested in any other solution, but I'll stick to that for now.