OnResume method is undoing the SharedPreferences of a CheckBox

0

It turns out that I want to leave the CheckBox saved according to whether the user checks it or not. So I called a method to check the internet if CheckBox is checked, and then CheckBox was mentioned in onResume to be checked updated mainly the internet check if the user quits and returns the application, but when mentioned onResume undoes the checked option, see:

checkBox = (CheckBox) findViewById(R.id.checkNetwork);
        checkBox.setChecked(getFromSP("checkBox"));
        checkBox.setOnCheckedChangeListener(this);

        checkBox2 = (CheckBox) findViewById(R.id.checkAlertDownload);
        checkBox2.setChecked(getFromSP("checkBox22"));
        checkBox2.setOnCheckedChangeListener(this);

        checkBox3 = (CheckBox) findViewById(R.id.checkAutoInstall);
        checkBox3.setChecked(getFromSP("checkBox33"));
        checkBox3.setOnCheckedChangeListener(this);

...

    private boolean getFromSP(String key) {
        SharedPreferences preferences = getApplicationContext
                ().getSharedPreferences("PROJECT_NAME", Context.MODE_PRIVATE);
        return preferences.getBoolean(key, false);
    }

    private void saveInSp(String key, boolean value) {
        SharedPreferences preferences = getApplicationContext
                ().getSharedPreferences("PROJECT_NAME", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putBoolean(key, value);
        editor.commit();
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean
            isChecked) {
        switch (buttonView.getId()) {
            case R.id.checkNetwork:
                saveInSp("checkBox", isChecked);
                checkConnection();
                break;
            case R.id.checkAlertDownload:
                saveInSp("checkBox22", isChecked);
                break;
            case R.id.checkAutoInstall:
                saveInSp("checkBox33", isChecked);
                break;
        }
    }

    private void checkConnection(){
        boolean isConnected = myNet.isConnected();
        showSnackBar(isConnected);
    }

    private void showSnackBar(boolean isConnected){
        String message;
        int color;
        if (isConnected) {
            message = "Good! Connected to internet";
            color = Color.WHITE;
        } else {
            message = "Sorry! Not connected to internet";
            color = Color.RED;
        }

        Toast.makeText(this, message, Toast.LENGTH_LONG).show();
    }

    @Override
    protected void onResume(){
        super.onResume();
        checkBox.setChecked(getFromSP("Key");
        MyApplication.getInstance().setConnectivityListener(this);
    }

    @Override
    public void onNetworkConnectionChanged(boolean isConnected){
        showSnackBar(isConnected);
    }
}

I think it's because onResume is calling the getFromSP method, where it is returning false - No option checked, strong> true - The CheckBox mentioned in onResume is always checked. Can someone help me? I've been breaking my head for some time. Many thanks!

    
asked by anonymous 23.07.2016 / 22:06

0 answers