デバイスがライト モードまたはその逆のときにアプリをダーク モードに切り替えると、フラグメントがちらつくため、デバイスがライト モードのときにアプリをダーク モードに切り替えると、BottomNavigationView (5 つのフラグメント) を含む MainActivity を開くと、最初のフラグメントが表示されますちらつきとして1秒間ライトモードになり、ダークモードに変換されます。
注:どのフラグメントにもダークモードコードの切り替えを追加していません
。AppCompatDelegate.setDefaultNightMode(pref.getStyleMode())すでに追加しようとしましたが、同じ問題が発生し、他の解決策をいくつか試しましたが、まだ同じことが起こっています!!
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/navView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
app:labelVisibilityMode="labeled"
app:itemIconTint="@drawable/bottom_nav_icon_color_selector"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:itemTextColor="@color/navigation_text_color"
app:menu="@menu/bottom_nav_menu" />
<FrameLayout
android:id="@+id/main_container"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@id/navView"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<androidx.appcompat.widget.AppCompatEditText
android:id="@+id/editText"
android:layout_width="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_height="wrap_content"
android:visibility="gone"/>
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.kt
override fun onCreate(savedInstanceState: Bundle?) {
AppCompatDelegate.setDefaultNightMode(pref.getStyleMode())
super.onCreate(savedInstanceState)
setContentView(getLayoutRes().layout)
bindView(savedInstanceState)
}
AppearanceActivity.kt
override fun onClick(v: View?) {
when (v?.id) {
consSystem.id -> {
if (pref.getStyleMode() == MODE_NIGHT_FOLLOW_SYSTEM) return
pref.setStyleMode(MODE_NIGHT_FOLLOW_SYSTEM)
updateUi(MODE_NIGHT_FOLLOW_SYSTEM)
setDefaultNightMode(MODE_NIGHT_FOLLOW_SYSTEM)
// delegate.localNightMode = MODE_NIGHT_FOLLOW_SYSTEM
}
consLight.id -> {
if (pref.getStyleMode() == MODE_NIGHT_NO) return
pref.setStyleMode(MODE_NIGHT_NO)
updateUi(MODE_NIGHT_NO)
setDefaultNightMode(MODE_NIGHT_NO)
// delegate.localNightMode = MODE_NIGHT_NO
}
consDark.id -> {
if (pref.getStyleMode() == MODE_NIGHT_YES) return
pref.setStyleMode(MODE_NIGHT_YES)
updateUi(MODE_NIGHT_YES)
setDefaultNightMode(MODE_NIGHT_YES)
// delegate.localNightMode = MODE_NIGHT_YES
}
}
}