Saving data in application

后端 未结 4 1939
梦如初夏
梦如初夏 2021-01-24 00:14

I have made an application. It\'s a button that shows the time you have pressed it. Every time I \"kill\" the application, the timer starts at 0 again (naturally). How can I mak

4条回答
  •  情书的邮戳
    2021-01-24 00:37

    Setting values in Preference:

    SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
     editor.putString("name", "slama");
     editor.putInt("idName", 28);
     editor.commit();
    

    Retrieve data from preference:

    // MY_PREFS_NAME - a static String variable like: 
    //public static final String MY_PREFS_NAME = "MyPrefsFile";
    SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE); 
    String restoredText = prefs.getString("text", null);
    if (restoredText != null) {
      String name = prefs.getString("name", "No name defined");//"No name defined" is the default value. String name = prefs.getString("name", "");
      int idName = prefs.getInt("idName", 0); //0 is the default value.
    }
    

    and this is an Android Shared Preferences Tutorial: http://www.tutorialspoint.com/android/android_shared_preferences.htm

    it can help you to solve your problem

提交回复
热议问题