MODE_NIGHT_FOLLOW_SYSTEM после MODE_NIGHT_NO - PullRequest
1 голос
/ 09 марта 2019

При переключении в режим MODE_NIGHT_FOLLOW_SYSTEM применяется последний использованный режим:

Приложение запускается в следующем режиме системы: стиль приложения светлый.

Я перехожу в ночной режим: стиль приложениятемно.

Я переключаюсь, чтобы перейти в режим системы: стиль приложения темный.

Я переключаюсь в дневной режим: стиль приложения светлый.

IПерехожу в режим системы: стиль приложения светлый.

SdkVersion: 28, протестировано на Android 4.0.3 (эмулятор) и 6.0.1 (реальное устройство).

Мой код:

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);

Активность:

<?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>

и стили:

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

Что я делаю не так?

1 Ответ

1 голос
/ 10 мая 2019

Это ошибка в AppCompat. Они отметили это исправленным в 1.1.0-alpha03: https://developer.android.com/jetpack/androidx/releases/appcompat#1.1.0-alpha03

...