Saving Form Data with

SharedPreferences

You can use the persistent storage mechanism called SharedPreferences to store the application game settings. Using these preferences, you can save all the form values on the settings screen.

Defining SharedPreferences Entries

Earlier you added a string to the QuizActivity base class for your game preferences:

public static final String GAME_PREFERENCES = "GamePrefs";

Now you need to add a preference String name for each of the values you want to store to the QuizActivity class:

public static final String GAME_PREFERENCES_NICKNAME = "Nickname"; // String public static final String GAME_PREFERENCES_EMAIL = "Email"; // String public static final String GAME_PREFERENCES_PASSWORD = "Password"; // String public static final String GAME_PREFERENCES_DOB = "DOB"; // Long public static final String GAME_PREFERENCES_GENDER = "Gender"; // Int

Saving Settings to SharedPreferences

Now that you have defined the preference settings, you can save any committed form field to the game preferences. Within the QuizSettingsActivity class, you begin by defining a SharedPreferences member variable: SharedPreferences mGameSettings;

Within the onCreate() method of the activity, you initialize this member variable as follows: mGameSettings =

getSharedPreferences(GAME_PREFERENCES, Context.MODE_PRIVATE);

You pass in the name of your SharedPreferences (the String called GAME_PREF-ERENCES you created in the QuizActivity class). The mode called MODE_PRIVATE is the default permission used for private application files.

Now anyplace you need to save a preference, you simply open SharedPreferences.Editor, assign a specific preference setting, and commit the change. For example, to save the Nickname EditText information, you retrieve the text by using the EditText control's getText() method:

final EditText nicknameText =

(EditText) findViewById(R.id.EditText_Nickname); String strNickname = nicknameText.getText().toString();

After you have extracted the String value from the EditText input field, you can save it to SharedPreferences.Editor, using the putString() method: Editor editor = mGameSettings,edit();

editor.putString(GAME_PREFERENCES_NICKNAME, strNickname); editor.commit();

The Nickname, Email, and Password settings can be saved as String values, but the Date of Birth and Gender settings are of long and integer types, respectively. To save these settings, you must extract the value from the appropriate control, convert it if necessary, and save it using the SharedPreferences.Editor methods putLong() and putInt().

For now, you can commit the input from the Nickname, Email, and Gender fields. You will work further with the Date of Birth and Password fields in the next hour, when you implement the DatePickerDialog and Password Dialog window. If you go back through the QuizSettingsActivity class and look for the places where you have TODO comments, you can see exactly where you need to commit the data.

0 0

Post a comment