【发布时间】:2021-05-31 17:38:30
【问题描述】:
我试图部署一个切换按钮来将布局切换到浅色模式到深色模式,但我在这里遇到了一些问题。当我启动应用程序时,它不能自动切换到黑暗,我之前关闭应用程序之前已经切换到黑暗模式。当我回到设置页面时,它会切换到暗模式,但在这里我得到了错误。应用进入循环,
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);
}
}
}
【问题讨论】:
标签: java android firebase sharedpreferences