Android SharedPreferences String Set - some items are removed after app restart

前端 未结 6 1997
南笙
南笙 2021-02-03 19:38

I save a string set in the shared preferences, if I read it out it\'s ok. I start other activities, go back and read it again, it\'s ok. If I close the application, and start it

6条回答
  •  悲&欢浪女
    2021-02-03 20:18

    To save string in sharedprefernces

       SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(this).edit();
       editor.putString("text", mSaved.getText().toString());
       editor.commit();
    

    To retrieve data from shared preference

     SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
     String restoredText = prefs.getString("text", null);
    

提交回复
热议问题