The document discusses using shared preferences in Android to store data. It explains opening shared preferences in private mode, reading and writing values to shared preferences using methods like getString(), edit(), and putString(). It provides code examples of getting and setting username, password, and a checkbox value from shared preferences. The code shows initializing shared preferences, retrieving values on startup to populate fields, and committing edited values on a button click.
Disha NEET Physics Guide for classes 11 and 12.pdf
Develop Android Shared Preferences
1. Develop on Android
Android Lab Test
www.AndroidLabTest.com
Youku
By Bruno Delb
www.weibo.com/brunodelb
i.youku.com/brunodelb | www.weibo.com/brunodelb | blog.delb.cn
http://i.youku.com/brunoparis
Weibo
Officialsite
Lesson : The shared preferences
2. The shared preferences
• In this lesson, you will learn to store some
shared preferences.
• For this, you will use the
SharedPreferences.
3. The shared preferences
• Open the shared preferences « demo » in private mode :
SharedPreferences sharedPreferences = getSharedPreferences ("demo",
Context.MODE_PRIVATE);
• To read the preferences, use the method getString() for strings,
getBoolean() for booleans, etc …
et_username.setText (sharedPreferences.getString("username", ""));
cb_keepSession.setChecked (sharedPreferences.getBoolean ("keepSession", false));
4. The shared preferences
• To write the values in the shared preferences, call the method edit() to enter
in edition mode.
Editor editor = sharedPreferences.edit();
• Then use the methods putString(), putBoolean(), etc … depending on the
type of each information.
editor.putString("username", et_username.getText().toString());
editor.putString("password", et_password.getText().toString());
editor.putBoolean("keepSession", cb_keepSession.isChecked());
• A the end, call the method commit() to validate the data.
editor.commit();
12. Follow me on my channel PengYooTV …
On my Youku channel
http://i.youku.com/brunoparis
Who am I ?
Bruno Delb (www.delb.cn),
Author of the first french book of development of Java mobile application (2002),
Consultant, project manager and developer of social & mobile applications,
let’s talk about your needs ...
And on Weibo :
http://www.weibo.com/brunodelb