save variables after quitting application?

前端 未结 3 1966
说谎
说谎 2020-12-08 15:34

I want some variables to be saved, when I shut down my app and to load them after opening the app (for statistics in a game)

How can I do this?

EDIT: Here my

3条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-08 16:13

    sample code: save:

    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
    SharedPreferences.Editor editor = settings.edit();
    editor.putString("statepara1", ts);
    editor.commit();
    

    get:

    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);    
    String ret = settings.getString("statepara1", "0");
    

提交回复
热议问题