Creating and Managing Activity Level Shared preferences in Android.

useful video : https://youtu.be/RKX_htOY6bo

The Activity which Creates the Preference is the Only Activity, who can Acess it.

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

Popular posts from this blog

React Js + React-Redux (part-2)

React Js + CSS Styling + React Router (part-1)

ViteJS (Module Bundlers, Build Tools)