Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to store data in shared Preference and to fetch the stored data

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 153
    Comment on it

    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)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: