Q:

android.widget.Swtich - Keep state on pause?

I am a new developer writing an app that logs location when a switch is toggled. My switch works when I toggle it, but it loses it's state when I take the app out of the forefront. How do I keep the state of my android switch onPause and onResume?
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.content_main);

        // set view, initiate switch
        Switch mainSwitch = (Switch) findViewById(R.id.mainSwitch);

        // deal with switch
        mainSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @SuppressLint("MissingPermission")
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                // if switch is on
                if (isChecked) {
                        logLocation();
                    }
                } else if (!isChecked) {
                    stopLocation();
                }
                // true if the switch is in the On position
                Log.v("@@@@@@@@@@@@@=", "" + isChecked);
            }
        });
    }

A:

Adding an answer instead of continuing in the comments.
By default Android will save the state of your UI if and only if you're using default components (meaning TextView, Button, Switch etc.) and your component has an id. This state will only survive if the OS kills the app and the user returns to it. This means that if the user presses the Home button and then returns to your Activity at a later stage, the UI state of all default components of that specific Activity will be restored. When pressing the Home button the Activity is stopped and onSaveInstanceState is called, so here you get a chance to save any state that isn't maintained by default components or if you have some data you downloaded for instance that is used to populate your UI components with data.
If the user presses the Back button the app will be killed and any UI state won't be restored per default.
To overcome this, you can use SharedPreferences or any other local storage mechanism to store your UI state to disk basically.
I've added a snippet of code, that shows how you can use the different approaches.
In your case you don't need the onSaveInstanceState method implementation, as you have a default UI component, that knows how to save its own state already and as mentioned Android does this automagically for you already.
Both states (savedInstanceState and what goes into the SharedPreferences) should be restored in the onCreate method as this is always called whenever an Activity is re-created or created the first time, while onRestoreInstanceState might not be called all the time.
public class MainActivity extends AppCompatActivity {
    private static final String TAG = MainActivity.class.getName();
    private static final String CHECKBOX__STATE = "CHECKBOX__SAVE_STATE";

    private Switch checkbox;

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

        checkbox = findViewById(R.id.checkbox);

        final boolean isChecked;
        if (savedInstanceState != null) {
            isChecked = savedInstanceState.getBoolean(CHECKBOX__STATE);
        } else {
            isChecked = PreferenceManager.getDefaultSharedPreferences(this)
                    .getBoolean(CHECKBOX__STATE, false);
        }

        // Setting the state (if any) of the Switch.
        checkbox.setChecked(isChecked);

        checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    Log.d(TAG, "onCheckedChanged: isChecked = true - log location");
                } else {
                    Log.d(TAG, "onCheckedChanged: isChecked = false - stop logging location");
                }
            }
        });
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putBoolean(CHECKBOX__STATE, checkbox.isChecked());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        PreferenceManager.getDefaultSharedPreferences(this)
                .edit()
                .putBoolean(CHECKBOX__STATE, checkbox.isChecked())
                .apply();
    }
}

I hope this helps :-)

