Shared Preferences

Web Hosting
Application shared preferences allows you to save and retrieve data in key, value pair. 
Initialization of SharedPreference in Android:.

Application shared preferences can be fetched using getSharedPreferences() method.The following code can be used to get application shared preferences.


       SharedPreferences pref = getApplicationContext().getSharedPreferences(
                           "any_prefname"MODE_PRIVATE);

Available mode for shared preference:

I have a tips for you so that it's not hard to understand and easy to implement.
You must used a function for saving a data and displaying data.

Here's the code below:

private void SavePreferences(String key, String value) {
SharedPreferences sharedpreferences = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(key, value);
editor.commit();
}
Just call: SavePreferences("from", get_from ); in your activity when you need it.
private String LoadPreferences(String key, String value) {
    SharedPreferences sharedpreferences = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE);
    String savedValue = sharedpreferences.getString(key, value);
    return savedValue;
    }
Just call: LoadPreferences("from", ""); in your activity when you need it.


To edit sharedpreference value, we need editor to edit and save the changes in shared preferences.


Editor editor = pref.edit();
and to save data commit() is used.
Editor.commit();

You can save data into shared preferences using editor. All the primitive data types like booleans, floats, ints, longs, and strings are supported. Call editor.commit() in order to save changes to shared preferences.

              editor.putBoolean("key_name"true); // Storing boolean - true/false
              editor.putString("key_name""string value"); // Storing string
              editor.putInt("key_name""int value"); // Storing integer
              editor.putFloat("key_name""float value"); // Storing float
              editor.putLong("key_name""long value"); // Storing long
         editor.commit(); // commit changes


Get data from Shared Preference:

Data can be retrived from saved preferences by calling getString() (For string) method.for boolean getBoolean() Remember this method should be called on Shared Preferences not on Editor.

// returns stored preference value
// If value is not present return second param value - In this case null

              pref.getString("key_name"null); // getting String
              pref.getInt("key_name"null); // getting Integer
              pref.getFloat("key_name"null); // getting Float
              pref.getLong("key_name"null); // getting Long
              pref.getBoolean("key_name"null); // getting boolean

Delete data from shared preference and delete sharedpreference:
To delete data from shared preferences we can use remove(“key_name”).If we want to delete all the data, call clear()

editor.remove("student_name");//will delete student_name
editor.commit(); 

Following will clear all the data from shared preferences
editor.clear();
editor.commit();



Web Hosting

No comments:

Post a Comment