1
public class WordDisplay extends Activity {

private int level;      
private int group;      
private int set;        

private WordDisplay mContext=this;

private int l;
private int g;
private int s;

SharedPreferences preferences;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.wordset);
    set_Word_Display_Event();
    loadPreferences();

          }
protected void loadPreferences() {
 preferences = PreferenceManager.getDefaultSharedPreferences(this);
    // preferences = getSharedPreferences("one", Context.MODE_PRIVATE);

     l= preferences.getInt("Level", 0);
     g=preferences.getInt("Group", 0);
     s= preferences.getInt("Set", 0);
    // Log.d("lll"," - "+preferences.getInt("level",0));

}

    @Override
protected void onStop() {
    super.onStop();
    savePreferences(this.level,this.group,this.set);
 }

protected void savePreferences(int level, int group, int set) {
         preferences = PreferenceManager.getDefaultSharedPreferences(this);
        //preferences = getSharedPreferences("one", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();

        editor.putInt("Level", l);
        editor.putInt("Group", g);
        editor.putInt("Set", s);

       editor.commit();

        //return getPreferences(s).getInt("Set", 0);

    }
 }

Here, my data could not persist properly. what is the wrong my code. please give good convenience. Comments of the above code, Also checking but could not any effect.

Horrorgoogle
  • 7,858
  • 11
  • 48
  • 81

2 Answers2

1

First, onStop() "might" never be called (see Activity life cycle), practice is to save your data in the onPause() method.

Maybe try to add more logs to see what's going on?

  • is onStop() called?
  • what are the saved / loaded values?
  • etc.
DonGru
  • 13,532
  • 8
  • 45
  • 55
darma
  • 4,687
  • 1
  • 24
  • 25
  • you mean, onStop() method change into onPause() method. am i right? i do this but same problem comes again. – Horrorgoogle Aug 15 '11 at 09:37
  • Yes i meant that about onPause(). Besides that i don't see anything wrong, but since you're not posting the code where l,g,s are modified then i'm only suggesting the logs. Show more code about those l,g,s updates? – darma Aug 15 '11 at 09:41
  • 1
    In the savePreferences method, why not save the level,group,set values from the in-arguments? Why use l,g,s instead and where do those come from? Since you're not posting all the code then it's hard to tell, but it seems strange to have l,g,s AND level,group,set ALL as private members ; isn't there a problem? – darma Aug 15 '11 at 10:02
  • wow Nice, i changed insted of l,g,s to level, group,set, i sloved problem and I am also forged call method set_Word_Display_Method(). Thanks sir. – Horrorgoogle Aug 15 '11 at 10:06
0

Try it with a final string constant in your class:

public static final String PREFS_NAME = "MyPreferences";

and then always use the member function:

getSharedPreferences(PREFS_NAME, 0);

normally there is no magic to do.

http://developer.android.com/guide/topics/data/data-storage.html#pref

berlindev
  • 1,753
  • 14
  • 22