Chmyaf
Chmyaf

Reputation: 27

MODE_NIGHT_FOLLOW_SYSTEM after MODE_NIGHT_NO

When switching to MODE_NIGHT_FOLLOW_SYSTEM mode, the last used mode is applied:

Application started in follow system mode: app style is light.

I'm switching to night mode: app style is dark.

I'm switchng back to follow system mode: app style is dark.

I'm switching to day mode: app style is light.

I'm switchng back to follow system mode: app style is light.

SdkVersion: 28, tested on Android 4.0.3 (emulator) and 6.0.1 (real device).

My code:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView currentText = findViewById(R.id.current);
        currentText.setText("Current: " + AppCompatDelegate.getDefaultNightMode());
    }

    private void changeNightMode(int nightMode) {
        AppCompatDelegate.setDefaultNightMode(nightMode);
        recreate();
    }

    public void onSystem(View view) {
        this.changeNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
    }
    public void onNo(View view) {
        this.changeNightMode(AppCompatDelegate.MODE_NIGHT_NO);
    }
    public void onYes(View view) {
        this.changeNightMode(AppCompatDelegate.MODE_NIGHT_YES);
    }
    public void onAuto(View view) {
        this.changeNightMode(AppCompatDelegate.MODE_NIGHT_AUTO);

Activity:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/current"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Current"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/system"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onSystem"
        android:text="System"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/current" />

    <Button
        android:id="@+id/no"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onNo"
        android:text="No"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/system" />

    <Button
        android:id="@+id/yes"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onYes"
        android:text="Yes"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/no" />

    <Button
        android:id="@+id/auto"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onAuto"
        android:text="Auto"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/yes" />

</android.support.constraint.ConstraintLayout>

and styles:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.DayNight">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

</resources>

What am I doing wrong?

Upvotes: 2

Views: 1937

Answers (1)

Alexander Glenn
Alexander Glenn

Reputation: 51

This was a bug in AppCompat. They marked it fixed in 1.1.0-alpha03: https://developer.android.com/jetpack/androidx/releases/appcompat#1.1.0-alpha03

Upvotes: 4

Related Questions