Handle to a SharedPreferences
You can create a new shared preference file or access an existing one by calling one of two methods:
getSharedPreferences()
— Use this if you need multiple shared preference files identified by name, which you specify with the first parameter. You can call this from anyContext
in your app.getPreferences()
— Use this from anActivity
if you need to use only one shared preference file for the activity. Because this retrieves a default shared preference file that belongs to the activity, you don't need to supply a name.
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
Caution: If you create a shared preferences file with
MODE_WORLD_READABLE
or MODE_WORLD_WRITEABLE
, then any other apps that know the file identifier can access your data.Write to Shared Preferences
To write to a shared preferences file, create a
SharedPreferences.Editor
by calling edit()
on yourSharedPreferences
.
Pass the keys and values you want to write with methods such as
putInt()
and putString()
. Then callcommit()
to save the changes. For example:SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE); SharedPreferences.Editor editor = sharedPref.edit(); editor.putInt(getString(R.string.saved_high_score), newHighScore); editor.commit();
Read from Shared Preferences
To retrieve values from a shared preferences file, call methods such as getInt()
and getString()
, providing the key for the value you want, and optionally a default value to return if the key isn't present. For example:
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
int defaultValue = getResources().getInteger(R.string.saved_high_score_default);
long highScore = sharedPref.getInt(getString(R.string.saved_high_score), defaultValue);
Available mode for shared preference:
1. MODE_WORLD_READABLE
2. MODE_WORLD_WRITEABLE
3. MODE_PRIVATE
Store,Fetch,Remove,Clear Data from SharedPreferences.
/******* Create SharedPreferences *******/
SharedPreferences pref = getApplicationContext().getSharedPreferences(
"MyPref"
, MODE_PRIVATE);
Editor editor = pref.edit();
/**************** Storing data as KEY/VALUE pair *******************/
editor.putBoolean(
"key_name1"
,
true
);
// Saving boolean - true/false
editor.putInt(
"key_name2"
,
"int value"
);
// Saving integer
editor.putFloat(
"key_name3"
,
"float value"
);
// Saving float
editor.putLong(
"key_name4"
,
"long value"
);
// Saving long
editor.putString(
"key_name5"
,
"string value"
);
// Saving string
// Save the changes in SharedPreferences
editor.commit();
// commit changes
/**************** Get SharedPreferences data *******************/
// If value for key not exist then return second param value - In this case null
pref.getBoolean(
"key_name1"
,
null
);
// getting boolean
pref.getInt(
"key_name2"
,
null
);
// getting Integer
pref.getFloat(
"key_name3"
,
null
);
// getting Float
pref.getLong(
"key_name4"
,
null
);
// getting Long
pref.getString(
"key_name5"
,
null
);
// getting String
/************ Deleting Key value from SharedPreferences *****************/
editor.remove(
"key_name3"
);
// will delete key key_name3
editor.remove(
"key_name4"
);
// will delete key key_name4
// Save the changes in SharedPreferences
editor.commit();
// commit changes
/************ Clear all data from SharedPreferences *****************/
editor.clear();
editor.commit();
// commit changes
we create shared preference fileto save currrent state of activity
for example:
onPause()
{
SharedPreferences.Editor editor = getSharedPreferences(file_name,MODE).edit();
or
SharedPreferences.Editor editor = getPreferences(MODE).edit(); // editing data stored in default preference file
//store data that we want to persist using edit.putString(TAG,VALUE),edit.putInt(TAG,VALUE)
}
Now , whenever activity is restarted restore the persistent state by specifying the preference file used for storing persistent data when activity is in its resume state
for example :
onResume()
{
SharedPreferences pref = getSharedPreferences(file_name,MODE);
or
SharedPreferences pref = getPreferences(MODE); // used for storing data in default preference file
//retrieve the state using pref.getString(TAG) or pref.getInt(TAG,DEFAULTVALUE)
}
No comments:
Post a Comment