开发者

Saving Variables (Android)

I have a variable that I would like to save and be able to restore when the viewer opens the app back开发者_如何转开发 up. I call this variable, count

private int count=0;

It changes every now and then through out my main activity. How can I save this after editing and changing it and be able to restore it?


Using this...

protected void onResume(){
    super.onResume();
    SharedPreferences settings = getSharedPreferences(PREFS_COUNT, 0);
    count = settings.getInt("count", count);
}
protected void onPause(){
   super.onPause();


  SharedPreferences settings = getSharedPreferences(PREFS_COUNT, 0);
  SharedPreferences.Editor editor = settings.edit();
  editor.putInt("count", count);
  editor.commit();
}


Lookup SharedPreferences in the documentation.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜