Android SharedPreferences allow us to store private primitive application data like boolean, long, double, int, String in the form key and value pair.
I. Available of SharedPreferences Mode
1. MODE_WORLD_READABLE = value equal 1 .This constant was deprecated in API level 17.
Creating world-readable files is very dangerous, and likely to cause security holes in applications.
2. MODE_WORLD_WRITEABLE = value equal 2 .This constant was deprecated in API level 17.
Creating world-writable files is very dangerous, and likely to cause security holes in applications.
3. MODE_PRIVATE = value equal 0. File creation mode: the default mode, where the created file can only be accessed by the calling application.
4. MODE_MULTI_PROCESS = value equal 4. This method will check for modification of preferences even if the sharedpreference instance has already been loaded
5. MODE_APPEND = value equal 32768. This will append the new preferences with the already exisiting preferences
6. MODE_ENABLE_WRITE_AHEAD_LOGGING = value equal 8. Database open flag. When it is set , it would enable write ahead logging by default.
II. How to get a handle to a SharedPreferences?
We can create a new shared preferences or access existing file one by using two methodsgetSharedPreferences() — Use this if you need multiple shared preference files identified by name, which you specify with the first parameter. You can call this from any Context in your app.
getPreferences() — Use this from an Activity 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.
Context context = getActivity(); SharedPreferences Pref = context.getSharedPreferences( getString(R.string.preference_file_key), Context.MODE_PRIVATE);
Store data to SharedPreferences
SharedPreferences pref = getActivity().getPreferences(Context.MODE_PRIVATE); SharedPreferences.Editor editor = sharedPref.edit();
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 change
Retrieving data from SharedPreferences
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
Delete Or Remove Data 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
III. Start with SharedPreferences Simple Project
follow this article we will create new sharedpreferences from xml file and shared data to other activity, In this case I used xml file for create EditText for input data and showed on other activity (second activity).I used menu for show preferences dialog to insert data.
1. Create new project File--> New --> Android Project rename your project as you want.
2. Open Project go to create menu for action with preferences menu /res/menu and follow with this code
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.sharedpreferencesandroid.SharedPreferencesMainActivity" >
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/action_settings"
app:showAsAction="never"/>
<item
android:id="@+id/action_pref"
android:title="@string/action_Pref"
app:showAsAction="never"/>
</menu>
3. Create xml folder for store pref.xml under /res just right click New Folder and hit finish button
4. Create xml file call pref.xml in folder /xml for create preferences Right Click on folder New Android XML File and select resource type as Preferences and type file name as you want and hit finish button. And follow with this code.
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<EditTextPreference
android:key="username"
android:summary="please enter your name here!"
android:title="Enter your Name:" >
</EditTextPreference>
<EditTextPreference
android:key="userpass"
android:summary="please enter your password"
android:title="Enter Your Password:" />
</PreferenceScreen>
0 Comments