java – Spontaneous color change background

Question:

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:
        setTheme(R.style.AppThemeLight);
        break;
    case 1:
        setTheme(R.style.AppThemeDark);
        break;
    default:
        break;
}

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.

UPD0

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>
</style>

// changes to the opposite color android:windowBackground

UPD1

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.

UPD2

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.

Answer:

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

Scroll to Top