Creating And Saving Preferences

To create or modify a Shared Preference, call getSharedPreferences on the application Context, passing in the name of the Shared Preference to change. Shared Preferences are shared across an application's components, but aren't available to other applications.

To modify a Shared Preference use the SharedPreferences.Editor class. Get the Editor object by calling edit on the Shared Preferences object you want to change. To save edits call commit on the Editor, as shown in Listing 6-1.

LISTING 6-1: Creating new Shared Preferences

// Retrieve an editor to modify the shared preferences. SharedPreferences.Editor editor = mySharedPreferences.edit()

// Store new primitive types in the shared preferences object. editor.putBoolean("isTrue", true); editor.putFloat("lastFloat", 1f); editor.putInt("wholeNumber", 2); editor.putLong("aNumber", 3l);

editor.putString("textEntryValue", "Not Empty");

// Commit the changes. editor.commit();

Was this article helpful?

0 0
Mobile Apps Made Easy

Mobile Apps Made Easy

Quick start guide to skyrocket your offline and online business success with mobile apps. If you know anything about mobile devices, you’ve probably heard that famous phrase coined by one of the mobile device’s most prolific creators proclaiming that there’s an app for pretty much everything.

Get My Free Training Guide

Post a comment