androidtextviewsavestatedarkmode

is there way to preserve the value of variable state to show in textview after switching between light and dark modes


i am new to this, i am trying to build app that counts how many times button is clicked i have a variable called count that keeps count of how many times a button is clicked and shows that in textview below the button and i have dark mode toggle button to switch between dark and light modes. initially in light mode i clicked button few times then it showed the count of number of times button clicked, then i switched to dark mode the count has been set to zero is there any way to preserve the value of variable state to show in textview after switching between light and dark modes? when i click dark mode or light mode the count value resets to 0 but i want the previous value which was in light mode...

here is my code

public class MainActivity extends AppCompatActivity {

Button button;
TextView textView1,textView2;
Switch aSwitch;
int count=0;
String light="light",dark="dark";

RelativeLayout relativeLayout;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    button = findViewById(R.id.button);
    textView1 = findViewById(R.id.remindmetext);
    textView2 = findViewById(R.id.countext);
    aSwitch = findViewById(R.id.switch1);
    relativeLayout = findViewById(R.id.relative);

    SharedPreferences sharedPreferences
            = getSharedPreferences(
            "sharedPrefs", MODE_PRIVATE);
    final SharedPreferences.Editor editor
            = sharedPreferences.edit();
    final boolean isDarkModeOn
            = sharedPreferences
            .getBoolean(
                    "isDarkModeOn", false);

    if (isDarkModeOn) {
        AppCompatDelegate
                .setDefaultNightMode(
                        AppCompatDelegate
                                .MODE_NIGHT_YES);
        aSwitch.setText(
                "Light");
    }
    else {
        AppCompatDelegate
                .setDefaultNightMode(
                        AppCompatDelegate
                                .MODE_NIGHT_NO);
        aSwitch
                .setText(
                        "Dark");
    }

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            count=count+1;
            textView2.setText(Integer.toString(count));
        }
    });

    aSwitch.setOnClickListener(
            new View.OnClickListener() {

                @Override
                public void onClick(View view)
                {
                    // When user taps the enable/disable
                    // dark mode button
                    if (isDarkModeOn) {

                        // if dark mode is on it
                        // will turn it off
                        AppCompatDelegate
                                .setDefaultNightMode(
                                        AppCompatDelegate
                                                .MODE_NIGHT_NO);
                        // it will set isDarkModeOn
                        // boolean to false
                        editor.putBoolean(
                                "isDarkModeOn", false);
                        editor.apply();

                        // change text of Button
                        aSwitch.setText(
                                "Dark");
                    }
                    else {

                        // if dark mode is off
                        // it will turn it on
                        AppCompatDelegate
                                .setDefaultNightMode(
                                        AppCompatDelegate
                                                .MODE_NIGHT_YES);

                        // it will set isDarkModeOn
                        // boolean to true
                        editor.putBoolean(
                                "isDarkModeOn", true);
                        editor.apply();

                        // change text of Button
                        aSwitch.setText(
                                "Light");
                    }
                }
            });

}

}

here is gif

gif


Solution

  • When you change your theme, all activities and fragments get recreated. See Configuration Changes. You can either:

    And load from them accordingly. For this simple case, using savedInstanceState is less of a hassle, as it ensures a null value on the first creation and non-null on config changes. Using SharedPreferences will be easy too.