Question

I have in my oncreate a dialog with a checkbox. This checkbox if checked makes dialog not shows the next time i open the application.. I've saved the checkbox state in the sharedpreferences and it goes, it shows me if i've checked or not the checkbox but the dialog still open on startup..What's wrong?

EDIT

// Dialog View checkBoxView = View.inflate(this, R.layout.checkbox, null); CheckBox checkBox = (CheckBox) checkBoxView.findViewById(R.id.checkbox); checkBox.setChecked(getFromSP("checkBox"));

checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

private void saveInSp(String key,boolean value){
    SharedPreferences preferences = getApplicationContext().getSharedPreferences("PROJECT_NAME", android.content.Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = preferences.edit();
    editor.putBoolean(key, value);
    editor.commit();
}
    @Override
    public void onCheckedChanged(CompoundButton buttonView,
            boolean isChecked) {
        // TODO Auto-generated method stub
        switch(buttonView.getId()){
        case R.id.checkbox:
        saveInSp("checkBox",isChecked);

        break;
        }
    }
}); 
checkBox.setText("Never show it");

SharedPreferences preferences = getApplicationContext().getSharedPreferences("PROJECT_NAME", android.content.Context.MODE_PRIVATE);

boolean hasBeenChecked = preferences.getBoolean("checkbox", false);

if (!hasBeenChecked) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle(" TITLE");
    builder.setMessage(" blablabla ")
           .setView(checkBoxView)
           .setCancelable(false)

           .setNeutralButton("Ok", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
               }
           }).show();

    } 
Was it helpful?

Solution

I would:

  • (non-essential) Remove the saveInSp from the anonymous OnCheckedChangeListener and add it to the activity, for instance
  • (non-essential) You can call saveInSp from the listener by referencing MyActivity.this.saveInSp
  • Then, before initializing and showing your alert...

    SharedPreferences preferences = getApplicationContext().getSharedPreferences("PROJECT_NAME", android.content.Context.MODE_PRIVATE);
    
    boolean hasBeenChecked = preferences.getBoolean("checkBox", false);
    
    if (!hasBeenChecked) {
    
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(" TITLEs");
        builder.setMessage(" blablabla ")
               .setView(checkBoxView)
               .setCancelable(false)
    
               .setNeutralButton("Ok", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                        dialog.cancel();
                   }
               }).show();
    }
    

OTHER TIPS

You have to check your saved value is shared preferences before calling AlertDialog.show().

Example:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle(" TITLEs");
            builder.setMessage(" blablabla ")
                   .setView(checkBoxView)
                   .setCancelable(false)

                   .setNeutralButton("Ok", new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();
                       }
                   }).show();

AlertDialog dialog = builder.create();

if (getFromSP("checkBox")) {
   dialog.show();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top