Creating and Managing Activity Level Shared preferences in Android.
useful video : https://youtu.be/RKX_htOY6bo
We use getPreferences() method in the Activity level instead of getSharedpreferences() in App level .
getPreferences() returns the preference of the Current Activity.
--------------------------------------------------------------------------------------
CREATE & INSERT DATA.
step 1]
use the getpreference() and get the preference of the current Activity.
Call the Editor
Put your key and Value using put*valuetype()
implement apply() on editor.
SharedPreferences preferences = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("SWITCHVAL",b);
editor.apply();
FETCH DATA :
step 1]
use the getpreference() and get the preference of the current Activity.
use the get*valuetype() method on that preference and pass the Key and the default value(incase value if unavailable)
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
Boolean ischecked = sharedPreferences.getBoolean("SWITCHVAL",false);
Comments
Post a Comment