Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

shared preferences not being saved

In my activity i am updating the user interface according to the preferences stored. code for updateUI is as follows:

private void updateUI()
{
    //preferences = getSharedPreferences(Select.PREF_FILE_NAME, MODE_PRIVATE);
    preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

    toggle = (Button)findViewById(R.id.toggleButton);
    incommingEdit = (Button)findViewById(R.id.IncommingEditButton);
    outgoingEdit = (Button)findViewById(R.id.outgoingEditButton);
    missedEdit = (Button)findViewById(R.id.missedEditButton);
    save = (Button)findViewById(R.id.saveButton);
    cancel = (Button)findViewById(R.id.cancelButton);
    incommingCheck = (CheckBox)findViewById(R.id.incommingCheck);
    outgoingCheck = (CheckBox)findViewById(R.id.outgoingCheck);
    missedCheck = (CheckBox)findViewById(R.id.missedCheck);
    incommingTextView = (TextView) findViewById(R.id.incommingTextView);
    outgoingTextView = (TextView) findViewById(R.id.outgoingTextView);
    missedTextView = (TextView) findViewById(R.id.missedTextView);

    //Disable all the edit buttons until their checkboxes are checked.
    incommingEdit.setEnabled(false);
    outgoingEdit.setEnabled(false);
    missedEdit.setEnabled(false);

    //Display the messages in the text views.
    incommingTextView.setText(preferences.getString("incommingMsgPhone", "Currently there are no messages saved."));
    outgoingTextView.setText(preferences.getString("outgoingMsgPhone", "Currently there are no messages saved."));
    missedTextView.setText(preferences.getString("missedMsgPhone", "Currently there are no messages saved."));

    //Check the check boxes.
    if(preferences.getInt("incommingPhone", 0) == Calls.INCOMING_TYPE)
    {
        incommingCheck.setChecked(true);
        incommingEdit.setEnabled(true);
    }

    if(preferences.getInt("outgoingPhone", 0) == Calls.OUTGOING_TYPE)
    {
        outgoingCheck.setChecked(true);
        outgoingEdit.setEnabled(true);
    }

    if(preferences.getInt("missedPhone", 0) == Calls.MISSED_TYPE)
    {
        missedCheck.setChecked(true);
        missedEdit.setEnabled(true);
    }

    //Check if the application is on or off and set the text of the button.
    //preferences = getSharedPreferences(Select.PREF_FILE_NAME, MODE_PRIVATE);
    boolean on = preferences.getBoolean("isOn", false);
    if(!on)
        toggle.setText("Turn On");
    else
        toggle.setText("Turn off");
}

Here is how i am saving all these preferences:

save.setOnClickListener(new OnClickListener() 
    {

        @Override
        public void onClick(View v) 
        {
            // TODO Auto-generated method stub
            //Save all in the preference file and exit.
            //preferences = getSharedPreferences(Select.PREF_FILE_NAME, MODE_PRIVATE);
            Editor editor = preferences.edit();
            editor.putInt("incommingPhone", incomming);
            editor.putInt("outgoingPhone", outgoing);
            editor.putInt("missedPhone", missed);

            editor.putString("incommingMsgPhone", incommingMsg);
            editor.putString("outgoingMsgPhone", outgoingMsg);
            editor.putString("missedMsgPhone", missedMsg);

            editor.commit();
            finish();
        }
    });

my UI is updated properly second time i run my application but around third or fourth time i get default preference values. i even tried using getdefaultpreferences instead of getsharedpreferences, but no luck.

like image 501
coders1290 Avatar asked May 17 '26 10:05

coders1290


2 Answers

I had a similar problem with getStringSet, the documatation helped there

Note that you must not modify the set instance returned by this call. The consistency of the stored data is not guaranteed if you do, nor is your ability to modify the instance at all.

like image 121
Markus Avatar answered May 19 '26 03:05

Markus


Try clearing your editor before using it. In other words do:

Editor editor = preferences.edit();
editor.clear();
editor.putInt("incommingPhone", incomming);
editor.commit();

I have had exactly same problem like yours and this worked for me. For my whole code sample see this post on my blog

like image 41
Jacek Milewski Avatar answered May 19 '26 03:05

Jacek Milewski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!