If you want to store and get data in and from Shared preferences, here is the code that can help you:-
Step 1:- Create the Preference and store the values in created preferences:-
Context context=getApplicationContext();
SharedPreferences sharedPreferences;
sharedPreferences=context.getSharedPreferences("My_Shared_Preference",MODE_PRIVATE);
SharedPreferences.Editor editor= sharedPreferences.edit();
editor.putString("user_name", "User");
editor.putInt("uId", 12);
editor.commit();
Step 2:- Retrieve the stored values from created preferences:-
sharedPreferences = getSharedPreferences("My_Shared_Preference", MODE_PRIVATE);
String name = prefs.getString("user_name", null); // null is the default vaue
int userId = prefs.getInt("uId", 0); // 0 is the default value
0 Comment(s)