Save Switch Button State via Shared Preferences When App is Closed
<p>In my Activity I have an Switch Button. I wanted to keep the State of the Switch Button when the App closes from the background.</p>
<p>The State of the Switch remains when the App is there in the Background but it goes back to the default (OFF) state when the app is cleared from the background.</p>
<p>I tried replicating the program from <a href="https://stackoverflow.com/questions/28584177/how-to-save-switch-button-state-in-android">here</a>. But I am still not able to maintain the state of the switch button.</p>
<pre><code>@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_settings);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    switch1 = (Switch) findViewById(R.id.switch1);

    SharedPreferences sharedPrefs = getSharedPreferences(&quot;com.example.xyz&quot;, MODE_PRIVATE);
    switch1.setChecked(sharedPrefs.getBoolean(&quot;NameOfThingToSave&quot;, true));


    switch1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (switch1.isChecked()) {

                SharedPreferences.Editor editor = getSharedPreferences(&quot;com.example.xyz&quot;, MODE_PRIVATE).edit();
                editor.putBoolean(&quot;NameOfThingToSave&quot;, true);
                editor.apply();
                switch1.setChecked(true);

                AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
                // Setting Dialog Title
                alertDialog.setTitle(&quot;Download all the Product's PDF.&quot;);
                // Setting Icon to Dialog
                alertDialog.setIcon(R.drawable.pdf_alert_dialog);

                // Setting Positive &quot;Yes&quot; Button
                alertDialog.setPositiveButton(&quot;CANCEL&quot;,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                switch1.setChecked(false);
                                SharedPreferences.Editor editor = getSharedPreferences(&quot;com.example.xyz&quot;, MODE_PRIVATE).edit();
                                editor.putBoolean(&quot;NameOfThingToSave&quot;, false);
                                editor.apply();

                                dialog.dismiss();
                            }
                        });
                // Setting Negative &quot;NO&quot; Button
                alertDialog.setNegativeButton(&quot;DOWNLOAD ALL &quot;,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {

                                SharedPreferences.Editor editor = getSharedPreferences(&quot;com.example.xyz&quot;, MODE_PRIVATE).edit();
                                editor.putBoolean(&quot;NameOfThingToSave&quot;, true);
                                editor.apply();

                                
                                AlertDialog.Builder alertDialog1 = new AlertDialog.Builder(context);
                                // Setting Dialog Title
                                alertDialog1.setTitle(&quot;Free storage Available:&quot; + megAvailable1 + &quot; MB&quot;);
                                alertDialog1.setMessage(&quot;File size to Download: POJO MB&quot;);
                                // Setting Icon to Dialog
                                alertDialog1.setIcon(R.drawable.pdf_alert_dialog);

                                alertDialog1.setPositiveButton(&quot;CANCEL&quot;,
                                        new DialogInterface.OnClickListener() {
                                            public void onClick(DialogInterface dialog1, int which) {
                                                switch1.setChecked(false);
                                                SharedPreferences.Editor editor = getSharedPreferences(&quot;com.example.xyz&quot;, MODE_PRIVATE).edit();
                                                editor.putBoolean(&quot;NameOfThingToSave&quot;, false);
                                                editor.apply();

                                                dialog1.dismiss();
                                            }
                                        });
                                // Setting Negative &quot;NO&quot; Button
                                alertDialog1.setNegativeButton(&quot;DOWNLOAD  &quot;,
                                        new DialogInterface.OnClickListener() {
                                            public void onClick(DialogInterface dialog1, int which) {

                                                getFeedDownload();

                                                SharedPreferences.Editor editor = getSharedPreferences(&quot;com.example.xyz&quot;, MODE_PRIVATE).edit();
                                                editor.putBoolean(&quot;NameOfThingToSave&quot;, true);
                                                editor.apply();

                                            }

                                        });
                                alertDialog1.show();
                            }
                        });

                // Showing Alert Message
                alertDialog.show();
            } else {
            }
        }
    });
</code></pre>
<p>Where am I going wrong?</p>