I would:
- (non-essential) Remove the
saveInSp
from the anonymousOnCheckedChangeListener
and add it to the activity, for instance - (non-essential) You can call
saveInSp
from the listener by referencingMyActivity.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(); }