第一次执行夜间模式代码,会触发本次重启
The first time the night mode code is executed, it will trigger the current reboot
第一次执行夜间模式代码时,它会重新启动Activity
第二次,代码将正常运行并对程序进行更改
我使用了以下代码:
public class Splash extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
new CheckedNightMode().execute();
}
private class CheckedNightMode extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... voids) {
SharedPreferences sharedpreferences = getApplicationContext().getSharedPreferences("Night_Mode", Context.MODE_PRIVATE);
boolean state = sharedpreferences.getBoolean("State", false);
if (state) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
} else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
checkLanguage();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
startActivity(new Intent(Splash.this, Main.class));
finish();
}
}, 3000);
}
private void checkLanguage() {
SharedPreferences sharedpreferences = getApplicationContext().getSharedPreferences("Language", Context.MODE_PRIVATE);
String checkedLanguage = sharedpreferences.getString("Select", "en");
setLanguageState(checkedLanguage);
}
private void setLanguageState(String language) {
Resources resources = getResources();
Configuration configuration = resources.getConfiguration();
DisplayMetrics displayMetrics = resources.getDisplayMetrics();
configuration.setLocale(Locale.forLanguageTag(language));
resources.updateConfiguration(configuration, displayMetrics);
}
}
}
这是我的 Styles.xml:
<style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorRed</item>
</style>
主题Theme.MaterialComponents.DayNight.NoActionBar
我试过了,还是一样
这是默认行为。由于资源已更改,android:configChanges 默认重新启动 activity。阅读 https://developer.android.com/guide/topics/resources/runtime-changes 以了解。从本质上讲,您遇到的是预期的行为,而不是错误,您可能应该重新启动 activity,否则您将不得不 reinstantiate/reconfigure 递归地查看每个视图,这将变得复杂和错误。
我的代码是正确的。 Google自己的文档中说如果要在运行时改变主题,当前的action会重新开始
Note: Starting with AppCompat v1.1.0, setDefaultNightMode() automatically recreates any started activities.
第一次执行夜间模式代码时,它会重新启动Activity
第二次,代码将正常运行并对程序进行更改
我使用了以下代码:
public class Splash extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
new CheckedNightMode().execute();
}
private class CheckedNightMode extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... voids) {
SharedPreferences sharedpreferences = getApplicationContext().getSharedPreferences("Night_Mode", Context.MODE_PRIVATE);
boolean state = sharedpreferences.getBoolean("State", false);
if (state) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
} else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
checkLanguage();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
startActivity(new Intent(Splash.this, Main.class));
finish();
}
}, 3000);
}
private void checkLanguage() {
SharedPreferences sharedpreferences = getApplicationContext().getSharedPreferences("Language", Context.MODE_PRIVATE);
String checkedLanguage = sharedpreferences.getString("Select", "en");
setLanguageState(checkedLanguage);
}
private void setLanguageState(String language) {
Resources resources = getResources();
Configuration configuration = resources.getConfiguration();
DisplayMetrics displayMetrics = resources.getDisplayMetrics();
configuration.setLocale(Locale.forLanguageTag(language));
resources.updateConfiguration(configuration, displayMetrics);
}
}
}
这是我的 Styles.xml:
<style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorRed</item>
</style>
主题Theme.MaterialComponents.DayNight.NoActionBar
我试过了,还是一样
这是默认行为。由于资源已更改,android:configChanges 默认重新启动 activity。阅读 https://developer.android.com/guide/topics/resources/runtime-changes 以了解。从本质上讲,您遇到的是预期的行为,而不是错误,您可能应该重新启动 activity,否则您将不得不 reinstantiate/reconfigure 递归地查看每个视图,这将变得复杂和错误。
我的代码是正确的。 Google自己的文档中说如果要在运行时改变主题,当前的action会重新开始
Note: Starting with AppCompat v1.1.0, setDefaultNightMode() automatically recreates any started activities.