[英]How can I correctly to develop the dark mode in my Android studio?
我試圖部署一個開關按鈕來將布局切換到淺色模式到深色模式,但我在這里遇到了一些問題。 當我啟動應用程序時,它不能自動切換到黑暗,我之前關閉應用程序之前已經切換到黑暗模式。 當我回到設置頁面時,它會切換到暗模式,但在這里我得到了錯誤。 應用程序陷入循環,
D/TagLifecycle: In the setting darkModeOff event
D/TagLifecycle: In the activity_main onDestroy() event
D/TagLifecycle: In the activity_main onCreate() event
D/TagLifecycle: In the activity_main onStart() event
D/TagLifecycle: In the activity_main onResume() event
D/TagLifecycle: In the activity_main onPause() event
D/TagLifecycle: In the activity_main onStop() event
D/TagLifecycle: In the activity_user_info onCreate() event
D/TagLifecycle: In the activity_setting onCreate() event
D/TagLifecycle: In the setting darkModeOn event
D/TagLifecycle: In the activity_setting onCreate() event
D/TagLifecycle: In the setting darkModeOff event
D/TagLifecycle: In the activity_main onDestroy() event
D/TagLifecycle: In the activity_main onCreate() event
D/TagLifecycle: In the activity_main onStart() event
D/TagLifecycle: In the activity_main onResume() event
D/TagLifecycle: In the activity_main onPause() event
D/TagLifecycle: In the activity_main onStop() event
D/TagLifecycle: In the activity_user_info onCreate() event
D/TagLifecycle: In the activity_setting onCreate() event
D/TagLifecycle: In the setting darkModeOn event
D/TagLifecycle: In the activity_setting onCreate() event
D/TagLifecycle: In the setting darkModeOff event
這是我的設置頁面代碼!
private Switch darkSwitch;
public static final String MyPREFERENCES= "nightModePrefs";
public static final String KEY_ISNIGHTMODE = "isNightMode";
SharedPreferences sharedPreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setting);
Log.d(TAG_LIFECYCLE, "In the activity_setting onCreate() event");
fbAuth = FirebaseAuth.getInstance();
fbStore = FirebaseFirestore.getInstance();
userID = fbAuth.getCurrentUser().getUid();
sharedPreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
darkSwitch = findViewById(R.id.swDark);
checkNightModeActivated();
darkSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
saveNightModeState(true);
recreate();
Log.d(TAG_LIFECYCLE, "In the setting darkModeOn event");
} else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
saveNightModeState(false);
recreate();
Log.d(TAG_LIFECYCLE, "In the setting darkModeOff event");
}
}
});
BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_nav);
bottomNavigationView.setSelectedItemId(R.id.settingActivity);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.mainActivity:
startActivity(new Intent(getApplicationContext(),MainActivity.class));
overridePendingTransition(0,0);
return true;
case R.id.mapActivity:
startActivity(new Intent(getApplicationContext(),MapActivity.class));
overridePendingTransition(0,0);
return true;
case R.id.userInfoActivity:
startActivity(new Intent(getApplicationContext(),UserInfoActivity.class));
overridePendingTransition(0,0);
return true;
case R.id.settingActivity:
return true;
}
return false;
}
});
}
private void saveNightModeState(boolean nightMode) {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean(KEY_ISNIGHTMODE, nightMode);
editor.apply();
}
public void checkNightModeActivated(){
if (sharedPreferences.getBoolean(KEY_ISNIGHTMODE, false)){
darkSwitch.setChecked(true);
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
} else {
darkSwitch.setChecked(false);
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
}
}
}
這里有很多錯誤的地方,你可以改進它。 我可以告訴你它循環的原因。
這就是問題所在:
首次啟動時,您正在使用此 function 調用checkNightModeActivated();
檢查夜間模式。
里面這個 function
public void checkNightModeActivated(){
if (sharedPreferences.getBoolean(KEY_ISNIGHTMODE, false)){
darkSwitch.setChecked(true);
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
} else {
darkSwitch.setChecked(false);
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
}
}
}
您正在標記darkSwitch.setChecked(true);
此外,您為此添加了一個 onCheckedListener,當您在此處將其標記為 true 時會觸發它。
其中onCheckedListener里面如下:
darkSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
saveNightModeState(true);
recreate();
Log.d(TAG_LIFECYCLE, "In the setting darkModeOn event");
} else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
saveNightModeState(false);
recreate();
Log.d(TAG_LIFECYCLE, "In the setting darkModeOff event");
}
}
});
您將再次從checkNightModeActivated();
再次被調用,我們被循環。
我會為您推薦的解決方案是將最初檢查和應用暗模式的邏輯轉移到您自己的個人 class 中,它擴展了Application()
class。 在它下面,您可以通過 SharedPreference 放置相同的檢查邏輯,並且由於Application()
class 即使您重新創建活動,您也不會卡住。 請注意,您還需要在 Manifest 中注冊上述 class。
第二個建議是使用某種按鈕來確認更改,而不是直接更改復選框。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.