简体   繁体   中英

Alert Dialog preference not saving

So i have this AlertDialog that pops up and says something right as the user first opens the app.

And then they have the option of clicking a CheckBox and disabling the dialog pop up the next time the app launches.

Enabling the CheckBox is not working correctly. when the app is closed fully down and then restarted it pops up the alert dialog again. Does anyone see anything wrong with what i have done?

 public static final String PREFS_NAME = "MyPrefsFile1";
 public CheckBox dontShowAgain;

 @Override
 public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    AlertDialog.Builder adb = new AlertDialog.Builder(this);
    LayoutInflater adbInflater = LayoutInflater.from(this);
    View eulaLayout = adbInflater.inflate(R.layout.custom_dialog, null);
    dontShowAgain = (CheckBox)eulaLayout.findViewById(R.id.checkBox1);
    adb.setView(eulaLayout);
    adb.setTitle("Alert Dialog");
    adb.setMessage("My Customer Alert Dialog Message Body");



    adb.setPositiveButton("Ok", new DialogInterface.OnClickListener()
  {

       //CheckBox Confirm for Alert Dialog
       public void onClick(DialogInterface dialog, int which) {

         String checkBoxResult = "NOT checked";
       if (dontShowAgain.isChecked())  checkBoxResult = "checked";
       SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
       SharedPreferences.Editor editor = settings.edit();
       editor.putString("skipMessage", checkBoxResult); 

            // Commit the edits!
    editor.commit();
    return; 
      }
   });

      adb.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

             public void onClick(DialogInterface dialog, int which)  {

                             // Action for 'Cancel' Button
                        String checkBoxResult = "NOT checked";
                            if (dontShowAgain.isChecked())  checkBoxResult = "checked";
                            SharedPreferences settings =    getSharedPreferences(PREFS_NAME, 0);
                        SharedPreferences.Editor editor = settings.edit();

                                    editor.putString("skipMessage", checkBoxResult);    
                        // Commit the edits!
                        editor.commit();
                          return;
                    }
    });

                    //Preferences For Alert Dialog
                    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
                String skipMessage = settings.getString("skipMessage", "NOT checked");
           if (skipMessage !=("checked") )
           adb.setIcon(R.drawable.icon);
           adb.show();

   }

Here below is my AlertDialog XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/layout_root"
          android:orientation="horizontal"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:padding="10dp"
          >
  <ImageView android:id="@+id/image"
           android:layout_width="wrap_content"
           android:layout_height="fill_parent"
           android:layout_marginRight="10dp"
           />

  <TextView
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:textColor="#FFF"
    android:textSize="5px" />

  <CheckBox
    android:id="@+id/checkBox1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Do Not Show Me This Again" />

</LinearLayout>

The issue I see there is that you should not be using == or != on Strings. Instead, use .equals, so it's be

if( !skipMessage.equals("checked") )

The only thing that doesn't happen if the preference skipMessage is "checked" is setting the icon to R.drawable.icon. Put the adb.show() into the if statement too (surround it with curly brackets).

if (!skipMessage.equals("checked") ) {
    adb.show();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM