Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jetpack compose check if a function has been run in preview mode

Is this possible to check if a function has been run in preview mode in Jetpack compose? I have a function that returns a proper string to use in the app but this function uses some objects which disable preview mode for @Composable components. What I could do is to pass val isPreview: Boolean = false flag to every component and then run a simplified function if the flag is true but this adds some boilerplate code to every composable.

like image 738
iknow Avatar asked Sep 02 '25 10:09

iknow


1 Answers

I usually do this by calling LocalInspectionMode.current to know if I'm in preview mode. It returns true if in preview mode.

Here's what the documentation says about LocalInspectionMode:

Inspectable mode CompositionLocal. True if the composition is composed inside an Inspectable component.

like image 57
Mofe Ejegi Avatar answered Sep 03 '25 22:09

Mofe Ejegi