调用后重新创建启动画面 AppCompatDelegate.setDefaultNightMode

Splash Screen Recreating After Calling AppCompatDelegate.setDefaultNightMode

我正在使用 AppCompatDelegate.setDefaultNightMode(mode); 在我的 Android 应用程序中设置夜间模式,只要用户在他们设备上的共享首选项中选择首选项配置的任何模式,现在当我使用当应用程序从启动画面 Activity 启动时,共享首选项设置 UI 模式,activity 正在重新创建,然后我的应用程序有 2 个实例,因为启动画面旨在登陆Activity.

这是我的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();
    }

如何避免创建多个着陆点实例Activity?

确保尽快致电 AppCompatDelegate.setDefaultNightMode()。例如。在调用 super.onCreate() 之前。理想情况下,您希望在 Application.onCreate() 中调用它。另外,请确保使用最新版本的 AppCompat(至少 1.1.0),否则您可能会遇到此问题。

有关详细信息,请参阅