dimanche 26 février 2017

Why the unchecked state ís not being saved?

I have an activity in which there is one checkBox, now the problem arises that when the checkBox is in checked state it remains checked no issues, But when i uncheck it, it revert back to its checked condition. Why is this happening? Here, is my code, i have used shared Preference.

 public class Kinematics extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, CompoundButton.OnCheckedChangeListener {
    private DrawerLayout drawerLayout;
    private NavigationView navigationView;
    private ActionBarDrawerToggle phy_law_toggel;
    private Toolbar toolbar;
    private WebView webView;
    private FloatingActionButton floatingActionButton;
    private RelativeLayout relativeLayout;
    private CheckBox checkBox;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_kinematics);
        Toolbar toolbar = (Toolbar) findViewById(R.id.phy_lawtoolbar);
        setSupportActionBar(toolbar);
        setTitle(R.string.Kinematics);
        relativeLayout = (RelativeLayout) findViewById(R.id.relative);
        drawerLayout = (DrawerLayout) findViewById(R.id.phy_draw);
        navigationView = (NavigationView) findViewById(R.id.nav_view_phy);
        phy_law_toggel = new ActionBarDrawerToggle(this, drawerLayout, R.string.open, R.string.Close);
        drawerLayout.addDrawerListener(phy_law_toggel);
        toolbar = (Toolbar) findViewById(R.id.phy_lawtoolbar);
        setSupportActionBar(toolbar);
        phy_law_toggel.syncState();
        checkBox = (CheckBox) findViewById(R.id.fav);
        checkBox.setChecked(getAsp("checkbox"));
        checkBox.setOnCheckedChangeListener(this);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        navigationView.setItemIconTintList(null);
        webView = (WebView) findViewById(R.id.phy_law_web);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setBuiltInZoomControls(false);
        webView.loadUrl("file:///android_asset/mathscribe/Kinematics.html");
        floatingActionButton = (FloatingActionButton) findViewById(R.id.fab);
        floatingActionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                webView.scrollTo(0, 0);
            }
        });
        navigationView.setNavigationItemSelectedListener(this);
        DataBaseHandler db = new DataBaseHandler(this);
        if (checkBox.isChecked()) {
            db.add_activity(this.getClass().getSimpleName());

        }


    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (phy_law_toggel.onOptionsItemSelected(item)) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.favourite) {
            startActivity(new Intent(getApplicationContext(), FavoritePage.class));
        }
        drawerLayout.closeDrawer(GravityCompat.START);
        return true;
    }


    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (checkBox.isChecked()) {
            Toast.makeText(getApplicationContext(), " checked",
                    Toast.LENGTH_LONG).show();
            saveInSp("checkbox", isChecked);
        } else if (!checkBox.isChecked()) {
            Toast.makeText(getApplicationContext(), "not checked",
                    Toast.LENGTH_LONG).show();
            saveInSp("checkbox", !isChecked);
        }

    }

    private boolean getAsp(String key) {
        SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("ALpit", android.content.Context.MODE_PRIVATE);
        return sharedPreferences.getBoolean(key, false);
    }

    private void saveInSp(String key, boolean value) {
        SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("ALpit", android.content.Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean(key, value);
        editor.apply();
    }


}




Aucun commentaire:

Enregistrer un commentaire