How do I store data in my android app?

This question already has an answer here:

  • Saving Android Activity state using Save Instance State 25 answers

  • For a simple piece of data such as high score you could easily save this in SharedPreferences. This is a simple key value pairing that persists across app launches and doesn't require a SQL database.

    To obtain a shared preference:

    SharedPreferences prefs = this.getSharedPreferences( "com.example.app", Context.MODE_PRIVATE);
    

    To edit and save to shared preference:

    int highScore = 100; prefs.edit().putInt("HighScore", highScore).apply();
    

    To retrieve shared preference :

    int score = prefs.getInt("HighScore", 0);
    
    链接地址: http://www.djcxy.com/p/26188.html

    上一篇: 如何保存活动的状态?

    下一篇: 如何将数据存储在我的android应用程序中?