Search code examples
javaandroidandroid-studioandroid-night-mode

The first time the night mode code is executed, it will trigger the current reboot


The first time the Night Mode code is executed, it restarts the Activity

The second time, the code will work correctly and make changes to the program

I used the following code:

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);
        }
    }
}

this is my 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>

With the theme Theme.MaterialComponents.DayNight.NoActionBar I tried, but it was still the same


Solution

  • My code is correct. Google's own document states that if you want to change the theme at runtime, the current action will be restarted.

    Note: Starting with AppCompat v1.1.0, setDefaultNightMode() automatically recreates any started activities.