java、Android 中的 运行 app 是否可以在不重置的情况下使用 switchCompat?
Is there anyway to use switchCompat without resetting while running app in java, Android?
我正在安装秒表应用程序。
我在主布局上有不同背景的 Switchcompat。
当我在 运行 的时候点击开关时,它被重置为 00:00:00.
有没有什么方法可以在不重置定时器的情况下使用开关?
下面是部分代码。
MainActivity
SwitchCompat switchCompat;
SharedPreferences sharedPreferences = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
switchCompat = findViewById(R.id.switchCompat);
sharedPreferences = getSharedPreferences("night", 0);
Boolean booleanValue = sharedPreferences.getBoolean("NightMode", false);
if (booleanValue) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
switchCompat.setChecked(true);
}
switchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
switchCompat.setChecked(true);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("NightMode", true);
editor.commit();
}else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
switchCompat.setChecked(false);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("NightMode", false);
editor.commit();
}
}
});
}
签出 saving instance state pattern - 存储时间值并在 Activity
重新创建时重新存储
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putString("SwitchText", switchCompat.getText().toString());
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
switchCompat = findViewById(R.id.switchCompat);
if (savedInstanceState != null) { // is null on first start
String switchText = savedInstanceState.getString("SwitchText");
// may be null when no value stored under given key
if (switchText != null) switchCompat.setText(switchText);
}
...
Activity
得到 recreated/restared 因为在 onCheckedChanged
中你正在调用 AppCompatDelegate.setDefaultNightMode
- 这迫使 Activity
重新启动并应用新主题,你有编写一些额外的代码来保存以前的状态
我正在安装秒表应用程序。 我在主布局上有不同背景的 Switchcompat。 当我在 运行 的时候点击开关时,它被重置为 00:00:00.
有没有什么方法可以在不重置定时器的情况下使用开关?
下面是部分代码。
MainActivity
SwitchCompat switchCompat;
SharedPreferences sharedPreferences = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
switchCompat = findViewById(R.id.switchCompat);
sharedPreferences = getSharedPreferences("night", 0);
Boolean booleanValue = sharedPreferences.getBoolean("NightMode", false);
if (booleanValue) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
switchCompat.setChecked(true);
}
switchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
switchCompat.setChecked(true);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("NightMode", true);
editor.commit();
}else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
switchCompat.setChecked(false);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("NightMode", false);
editor.commit();
}
}
});
}
签出 saving instance state pattern - 存储时间值并在 Activity
重新创建时重新存储
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putString("SwitchText", switchCompat.getText().toString());
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
switchCompat = findViewById(R.id.switchCompat);
if (savedInstanceState != null) { // is null on first start
String switchText = savedInstanceState.getString("SwitchText");
// may be null when no value stored under given key
if (switchText != null) switchCompat.setText(switchText);
}
...
Activity
得到 recreated/restared 因为在 onCheckedChanged
中你正在调用 AppCompatDelegate.setDefaultNightMode
- 这迫使 Activity
重新启动并应用新主题,你有编写一些额外的代码来保存以前的状态