Dev.to
5/11/2026

My Compose App Was Showing the Wrong Theme After Restart — Here's the Fix
Short summary
Android Compose theme resets to light on every cold start if you only use StateFlow. The fix: persist to SharedPreferences when setTheme() is called, read on ViewModel initialization. This post covers ThemeMode enum with system follow option, why SharedPreferences beats DataStore for sync reads, M3 colorScheme factory functions, and CompositionLocal pattern for semantic colors beyond M3's 30 standard roles.
- •Theme persistence requires StateFlow updates plus SharedPreferences writes—StateFlow alone causes resets on cold start
- •Use ThemeMode enum (LIGHT, DARK, SYSTEM) with synchronous SharedPreferences access for immediate read availability
- •Leverage isSystemInDarkTheme() for reactive system theme following and CompositionLocal for semantic colors
Generated with AI, which can make mistakes.
Is this a good recommendation for you?



