java – Spontaneous color change background


There is an application for Android. Built on Activity + Fragment . The application (in the style.xml styles) implements two themes: light and dark. In onCreate Activity before the setContentView() method, as well as in onResume() simple code is written:

// style - int переменная, которой присваивается 0/1 из SharedPreferences
switch (style) {
    case 0:
    case 1:

The problem is as follows. When the user minimizes the application for a long time, then after the unfolding, the background color ( background ) changes to the opposite (from a different style). The colors for Toolbar , NavigationDrawer , primaryTextColor , secondaryTextColor and others remain in place.

What could be the problem ?

PS: the first solution was to move the block of code for changing the style from Activity into the MyAppClass class, which is extended by Activity and is written in the manifest. But there was no sense in it.


style.xml -> LightTheme

 <style name="AppThemeLight" parent="Theme.AppCompat.Light.NoActionBar" >
    <item name="colorPrimary">@color/aa_primaryColor_lightTheme</item>
    <item name="colorPrimaryDark">@color/aa_primaryDarkColor_lightTheme</item>
    <item name="colorAccent">@color/aa_accentColor_lightTheme</item>
    <item name="android:colorBackground">@color/aa_backgroundColor_lightTheme</item>
    <item name="android:colorForeground">@color/aa_foregroundColor_lightTheme</item>
    <item name="android:textColorPrimary">@color/aa_primaryTextColor_lightTheme</item>
    <item name="android:textColorSecondary">@color/aa_secondaryTextColor_lightTheme</item>
    <item name="android:windowBackground">@color/aa_windowBackgroundColor_lightTheme</item>

// changes to the opposite color android:windowBackground


But, if you set the Light style, then after spending a long time in the minimized form, the application does not change its android:windowBackground to the opposite. The only problem is the dark scheme.


If you swap the Light and Dark themes in the switch-case , then everything equal after a long idle time the screen turns white, and everything else is as expected.


The style must be assigned before calling super.onCreate(savedInstanceState); in the onCreate method of the onCreate class.

Scroll to Top