- Android Application Development Cookbook(Second Edition)
- Rick Boyer Kyle Mew
- 398字
- 2021-07-09 19:36:20
Storing persistent activity data
Being able to store information about our activities on a temporary basis is very useful, but more often than not, we will want our application to remember information across multiple sessions.
Android supports SQLite, but that could be a lot of overhead for simple data, such as the user's name or a high score. Fortunately, Android also provides a lightweight option for these scenarios, with SharedPreferences
.
Getting ready
You can either use the project from the previous recipe or start a new project and call it PersistentData
(in a real-world application, you'll likely be doing both anyway). In the previous recipe, we saved mCounter
in the session state. In this recipe, we'll add a new method to handle onPause()
and save mCounter
to SharedPreferences
. We'll restore the value in onCreate()
.
How to do it...
We have only two changes to make, and both are in MainActivity.java
:
- Add the following
onPause()
method to save the data before the activity closes:@Override protected void onPause() { super.onPause(); SharedPreferences settings = getPreferences(MODE_PRIVATE); SharedPreferences.Editor editor = settings.edit(); editor.putInt(KEY_COUNTER, mCounter); editor.commit(); }
- Then add the following code at the end of
onCreate()
to restore the counter:SharedPreferences settings = getPreferences(MODE_PRIVATE); int defaultCounter = 0; mCounter = settings.getInt(KEY_COUNTER, defaultCounter);
- Run the program and try it out.
How it works...
As you can see, this is very similar to saving state data, because it also uses name/value pairs. Here, we just stored an int
, but we can just as easily store one of the other primitive data types. Each data type has equivalent getters and setters, for example, SharedPreferences.getBoolean()
or SharedPreferences.setString()
.
Saving our data requires the services of SharedPreferences.Editor
. This is evoked with edit()
and accepts remove()
and clear()
procedures as well as setters such as putInt()
. Note that we must conclude any storing that we do here with the commit()
statement.
There's more...
There is a slightly more sophisticated variant of the getPreferences()
accessor: getSharedPreferences()
. It can be used to store multiple preference sets.
Using more than one preference file
Using getSharedPreferences()
is no different from using its counterpart, but it allows for more than one preference file. It takes the following form:
getSharedPreferences(String name, int mode)
Here, name
is the file. The mode
can be either MODE_PRIVATE
, MODE_WORLD_READABLE
, or MODE_WORLD_WRITABLE
and describes the file's access levels.
See also
- Chapter 6, Working with Data, for more examples on data storage
- 黑客攻防從入門到精通(實戰秘笈版)
- Hands-On Machine Learning with scikit:learn and Scientific Python Toolkits
- Xcode 7 Essentials(Second Edition)
- Responsive Web Design with HTML5 and CSS3
- 信息安全技術
- Flash CS6中文版應用教程(第三版)
- 微信公眾平臺開發:從零基礎到ThinkPHP5高性能框架實踐
- SharePoint Development with the SharePoint Framework
- The Complete Coding Interview Guide in Java
- Nagios Core Administration Cookbook(Second Edition)
- Mastering Concurrency Programming with Java 9(Second Edition)
- Java 9 with JShell
- 算法秘籍
- Java EE架構設計與開發實踐
- Drupal 8 Development Cookbook(Second Edition)