Search code examples
javaandroidswitch-statementswitchcompat

Android - SwitchCompat OnCheckedChangeListener action is running every time the activity is started


I have some SwitchCompat in my Activity, I set the OnCheckedChangeListener to one of them but (using SharedPreferences), every time I start the Activity, the action of OnCheckedChangeListener is performed regardless of whether it is on or off (Which is very very bad for performance as the On state action is to run a shell script and show a SnackBar as it takes some time).

Here is a small piece of code...

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
        //Private stuffs...
        SwitchCompat play; //and many others
        public static final String PREFS_NAME = "SwitchButton";

        protected void onCreate(Bundle savedInstanceState) {
        // ...
        play = (SwitchCompat) findViewById(R.id.play_switch);
        play.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    Shell.SU.run("sh /data/data/br.com.packagename/play_on");
                    Snackbar snack_play_on = Snackbar.make(play, R.string.play_on, Snackbar.LENGTH_SHORT);
                    snack_play_on.show();

                    SharedPreferences.Editor editor = getSharedPreferences("SwitchButton", MODE_PRIVATE).edit();
                    editor.putBoolean("onPlay", true);
                    editor.apply();

                } else {
                    Shell.SU.run("sh /data/data/br.com.packagename/play_off");
                    SharedPreferences.Editor editor = getSharedPreferences("SwitchButton", MODE_PRIVATE).edit();
                    editor.putBoolean("onPlay", false);
                    editor.apply();

                    Snackbar snack_play_off = Snackbar.make(play, R.string.play_off, Snackbar.LENGTH_SHORT);
                    snack_play_off.show();
                }
            }
        });
        play.setChecked(sharedPrefs.getBoolean("onPlay", false));

So... Every time a open the activity (ot the app itself) that Snackbar shows, and the linked action to the On state of the SwitchCompat runs. This causes too many frames skip when loading the Activity (arround to 230 in a 1GB, 1.2GHz quad device). There is more then one Switch, four or five.

What should I do? Am I missing soomething or putting the code in the wrong place? Should I use other methods like OnResume, OnPause etc?


Solution

  • Calling setChecked() invokes the listener they same way a user click would.

    I now handle setting up all checkboxes like this:

            play = (SwitchCompat) findViewById(R.id.play_switch);
            play.setOnCheckedChangeListener(null);
            play.setChecked(sharedPrefs.getBoolean("onPlay", false));
            play.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
               ...