0

I need to check the user input in the preferences if they want to save a new value. I believe the code is correct... because if I'm change the values slowly there's no problem at all. If I change too quick the app is crashing.

i'm using commit() for editing the preferences. Is this a too slow methode?

What I'm trying?: If a user gave an empty string, I give an alert dialog and intern I change the pref back to the old value. Here is the code:

public class Preferences extends PreferenceActivity implements OnSharedPreferenceChangeListener {
//save settings
    static String SMStext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            addPreferencesFromResource(R.xml.preferences);

            SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);

            //-- First get SMStext and save in Strings
            SMStext = sp.getString("SMSText", "0");

            //SharedPreferences.Editor prefEditor = sp.edit();
            sp.registerOnSharedPreferenceChangeListener(this);   

    }


  @Override
  public void onSharedPreferenceChanged(SharedPreferences sp, String key) {

    String smstriggerworderror = "String cannot be empty";

    if (key.equals("SMSText")) {

        if  sp.getString("SMSText", "0").equals(""){

            SharedPreferences.Editor prefEditor = sp.edit();

            prefEditor.putString("SMSText", SMStext);
            prefEditor.commit();
                //write current value back to value and refresh interface
            SMStext = sp.getString("SMSText", "0");
                //Show alert dialog
            showdialog(smstriggerworderror);
            //finish();

        }
        //write current value back to value
        SMStext = sp.getString("SMSText", "0");
    }


private void showdialog(String message) {
      //create alertbox
      AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
      alertbox.setMessage(message);
      alertbox.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
      // Click listener on the neutral button or alert box
          public void onClick(DialogInterface arg0, int arg1) {
              finish();
          }
      });

      // show the alert box
      alertbox.show();      
}
user1404924
  • 95
  • 1
  • 2
  • 11

1 Answers1

0

Inside of onSharedPreferenceChanged your doing a prefEditor.commit(); this will then call the onSharedPreferenceChanged listener, which wil then call commit() over and over until............... StackOverFlow error.

Fix:

Don't change your SharedPreferences inside of the OnSharedPreferencesChanged listener.

Blundell
  • 75,855
  • 30
  • 208
  • 233
  • Thanks... I believe you're right...I test it without going into the if (key.equals("SMSText")) statement... It's not crashing. You possibly saw that after that commit() I give a dialogbox...with clicking OK there is a finish(). I thought that this tread then ended... Well I need to search for another solution. The problem I tried to accomplish was that Android is first saving the new user value and afterwards is given the onSharedPreferenceChanged.... It would be nice if I could check it before android saved his sharedpreference... – user1404924 May 22 '12 at 21:48
  • I believe the problem is located somewhere in the dialog.... If I get the dialog out of the code it's working fine.... we searching ;) – user1404924 May 22 '12 at 22:10
  • mhhh it's the finish() !! Well I us him to reset everything. If I don't use him the user sees no saved value in the preference....but he is still there. So the value is not updated to the userinterface. How I overcome this issue?! – user1404924 May 22 '12 at 22:19
  • I believe I've got him. The main problem was the onPause() and onResume() @Override protected void onPause() { super.onPause(); SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this); sp.unregisterOnSharedPreferenceChangeListener(this); } – user1404924 May 22 '12 at 23:06