Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8115421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T03:28:49+00:00 2026-06-06T03:28:49+00:00

I am using shared preferences to save 3 string fields. I am opening shared

  • 0

I am using shared preferences to save 3 string fields. I am opening shared preferences everytime with following code in another class. I am calling this class via a function to save it.

mPrefs=context.getSharedPreferences(id, Context.MODE_PRIVATE);

After doing this, I am saving 3 fields which are id, name and surname of a person. Save process is successful. I am holding all of those 3 variables in an object which is called person. I can get info from shared memory. When I get the info for the first person with his Id, I can do it. I am writing it out to Log. it is successful. But after getting second person info with second person’s id, first person object is also second person. Shared preferences assigns all the variables to same value.

Any helps will be appreciated. Thanks

EDIT

personGet.setId(mPrefs.getString("id", null)); 
personGet.setName(mPrefs.getString("name", null)); 
personGet.setSurname(mPrefs.getString("surname", null)); 
// This is for saving to shared preferences. 

mPrefs = context.getSharedPreferences(person.getId(), Context.MODE_PRIVATE);
SharedPreferences.Editor editor = mPrefs.edit(); 
editor.putString("name", person.getName()); 
editor.putString("surname", person.getSurname()); 
editor.putString("id", person.getId()); 
// This is for saving. the "id" is unique for all users –
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T03:28:51+00:00Added an answer on June 6, 2026 at 3:28 am

    SharedPreferences is just a Map of Key Value pairs

    Yes this will save your id under the key “id”:

     editor.putString("id", "abc");
    

    But doing this will override it as the key “id” is the same:

     editor.putString("id", "zxy");
    

    So when you ask for the sharedPreference “id” you will get

     zxy
    

    EDIT

    Don’t forget to call commit(); on your sharedpreferences

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using SharedPreferences to persists Object fields. Take this example: class Item { private
I compiled the following code as a shared library using g++ -shared ... :
I'm using SharedPreferences to save two kinds of preference information. 1) General app preferences
I created a sample application using Android TextView with shared preferences . In my
I am using shared hosting on Windows. I need to run some code that
I am using boost shared pointers in my program, and I have a class
Im using the SharedPreferences in my app to retain the ability to save/retrieve string
I am using a method like this private static <T> void setPreference(String key, T
I want to save/recall an integer array using SharedPreferences . Is this possible?
I am using shared preferences to store the number of times my application has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.