0

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.

Bill Gary
  • 2,987
  • 2
  • 15
  • 19
  • Ask them if they clearing their data manually. – RussVirtuoso Oct 16 '13 at 02:57
  • How many checkbox do you have? Are you having distinct `have` value for each checkbox when you are saving the boolean into SharedPreference? – Lawrence Choy Oct 16 '13 at 03:02
  • Probably not related but never use `==` when checking boolean types. In your `myGetChecked` method just use `if(bHave)`. – Squonk Oct 16 '13 at 03:12
  • Where are you calling `myGetChecked()`? – JiTHiN Oct 16 '13 at 03:14
  • I only have 1 checkbox per activity, and only 1 activity can be active at a time. And I'll remove the `==` – Bill Gary Oct 16 '13 at 03:14
  • I'm calling `myGetChecked()` in `onResume()` – Bill Gary Oct 16 '13 at 03:15
  • Try putting `data = getSharedPreferences(filename, 0);` into your `onCheckedChanged(...)` method but also add that to the start of your `myGetChecked()` method before you call `data.getBoolean(have, false)` – Squonk Oct 16 '13 at 03:26
  • It's in my `onCreate()` already, would moving it make a difference? – Bill Gary Oct 16 '13 at 03:30
  • @BillGary : I always `getSharedPreferences(...)` every time I need to write or read data - I may be wrong and it was only a suggestion. In theory, `.commit()` will force preference updates but `SharedPreferences` are stored in an XML file. If the `data` field is holding a cached version then changes won't be reflected - that's why I always call `getSharedPreferences` each time I need to read them. In theory it shouldn't matter but it may depend on the Android version - not sure. – Squonk Oct 16 '13 at 03:35
  • _Probably not related but never use `==` when checking boolean types. In your `myGetChecked` method just use `if(bHave)` @Squonk_ --- On the contrary, probably, that's the issue: **the value of local variable, `bHave`, may not be in sync with what's in the shared prefs**. 1) Do not assign the value that you have read from shared preference to a local variable (actually, get rid of `bHave`) 2) In `onCheckedChanged()` just use `e.putBoolean(have, isChecked);` and get rid of `if/else` blocks 3) In `myGetChecked()` use `checkbox.setChecked(data.getBoolean(have, false));` and get rid of `if` block – ozbek Oct 16 '13 at 05:05
  • Heck, you may even not need the `myGetChecked()` method: just read the value from the shared prefs when you need. – ozbek Oct 16 '13 at 05:08
  • But are these reasons why only some users have their SharedPreferences deleted and others don't? Like I mentioned, I haven't been able to recreate the issue on the 2 actual devices I have. – Bill Gary Oct 16 '13 at 14:19
  • A user replied saying that it was fixed, but didn't say whether they had deleted the app before updating or cleared the data. This question can be considered closed. – Bill Gary Oct 21 '13 at 17:49

2 Answers2

-1

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

harsha.kuruwita
  • 1,315
  • 12
  • 21
-1

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.

Bill Gary
  • 2,987
  • 2
  • 15
  • 19