开发者

Saving Variables (Android)

开发者 https://www.devze.com 2023-01-20 00:55 出处:网络
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

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

精彩评论

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