Question

I have an app that uses SharedPreferences to keep track of the Users collection of items. But I've been getting complaints that they enter their items using a checkbox and when they restart the app their collection shows as empty.

Here is the code for the checkboxs

SharedPreferences data;

data = getSharedPreferences(filename, 0); 

checkbox = (CheckBox) findViewById(R.id.checkbox);

checkbox.setOnCheckedChangeListener(this);

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    // TODO Auto-generated method stub
    switch (buttonView.getId()) {
    case R.id.checkbox:
        bHave = checkbox.isChecked();
        if (bHave == true) {
            SharedPreferences.Editor e = data.edit();
            e.putBoolean(have, bHave);
            e.commit();
        } else {
            SharedPreferences.Editor e = data.edit();
            e.putBoolean(have, bHave);
            e.commit();
        }
        break;
    }
}

That writes to SharedPreferences as soon as the box is checked, so I know the data gets saved.

And here is the code to populate the checkbox

private void myGetChecked() {
    // TODO Auto-generated method stub

    bHave = data.getBoolean(have, false);

    if (bHave == true) {
        checkbox.setChecked(true);
    }
}

Which should get the data from the SharedPreferences.

A few points...

  1. It works on the 2 mobile devices i have to test on, a Droid X2 and an LG Spectrum 2.
  2. One's that mention it not working on, LG Optimus G, DROID RAZR HD, Desire HD and a Trac-Phone SGH-S959G.
  3. No errors are thrown, so I can't post a LogCat.
  4. I have double-checked and made sure all names match, in the keys and variables.
  5. I use the same preferences file for the whole app, but the way it's coded, only one activity reads/writes to the file at a time.

Anyone have any idea why some users are getting their data erased? And if you need any more info, let me know.

Was it helpful?

Solution 2

After I asked a few of the users whether they still had the issue, one replied that it was fixed. They did not say whether they had uninstalled the app before updating or whether they cleared the app data.

Looks like this wasn't an issue at all, which was why I couldn't replicate it.

OTHER TIPS

cool you can create SharedPreferences class inside of your package

import java.util.HashMap;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class SessionManager {

    // Shared Preferences
    SharedPreferences pref;

    // Editor for Shared preferences
    Editor editor;

    // Context
    Context _context;

    // Shared pref mode
    int PRIVATE_MODE = 0;

    // Sharedpref file name
    private static final String PREF_NAME = "stackoverflowreply";

    // All Shared Preferences Keys
    private static final String IS_CHEKD = "ischekd";



    // Constructor
    @SuppressLint("CommitPrefEdits")
    public SessionManager(Context context) {
        this._context = context;
        pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
        editor = pref.edit();
    }


    public void createchekdstatus(boolean ischekd) {
        // Storing login value as TRUE
        editor.putBoolean(IS_CHEKD, ischekd);


        // commit changes
        editor.commit();
    }






    public void unchekd() {
        // Clearing all data from Shared Preferences
        editor.clear();
        editor.commit();

    }

    /**
     * Quick check for login
     * **/

    public boolean getchekdstatus() {
        return pref.getBoolean(IS_CHEKD, false);
    }

}

then you can store your data in your activity

SessionManager session = new SessionManager(getApplicationContext());
session.createchekdstatus(true);

and then retrive data from SharedPreferences any of ur activity

essionManager session = new SessionManager(getApplicationContext());
boolean chekboxstatus = session.getchekdstatus();

enjoy

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top