Android 调用AppCompatDelegate.setDefaultNightMode后重新创建启动屏幕
我使用的是Android 调用AppCompatDelegate.setDefaultNightMode后重新创建启动屏幕,android,performance,android-studio,android-layout,Android,Performance,Android Studio,Android Layout,我使用的是AppCompatDelegate.setDefaultNightMode(模式)要在我的Android应用程序中设置夜间模式,每当用户在其设备上的共享首选项中选择首选项配置的任何模式时,现在当我使用共享首选项设置UI模式时,当应用程序从启动屏幕活动启动时,活动将被重新创建,然后有2个我的应用程序实例,当启动屏幕打算着陆时 这是我的SplashScreen.java @Override protected void onCreate(Bundle savedInstance
AppCompatDelegate.setDefaultNightMode(模式)
要在我的Android应用程序中设置夜间模式,每当用户在其设备上的共享首选项中选择首选项配置的任何模式时,现在当我使用共享首选项设置UI模式时,当应用程序从启动屏幕活动启动时,活动将被重新创建,然后有2个我的应用程序实例,当启动屏幕打算着陆时
这是我的SplashScreen.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
SharedPreferences prefs = getSharedPreferences(UI_MODE, MODE_PRIVATE);
name = prefs.getString("uiMode", "System");
applyUI();
fireSplashScreen();
}
private void applyUI() {
if (name.equals("Dark")){
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
}
else if (name.equals("Light")){
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
}
else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
}
private void fireSplashScreen() {
Intent i = new Intent(SplashScreen.this, Landing.class);
startActivity(i);
finish();
}
如何避免创建登录活动的多个实例?请确保尽快调用
AppCompatDelegate.setDefaultNightMode()
。例如,在调用super.onCreate()
之前。理想情况下,您希望在Application.onCreate()
中调用它。另外,请确保使用最新版本的AppCompat(至少1.1.0),否则您可能会遇到此问题
有关更多详细信息,请参阅