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

  • Home
  • SEARCH
  • 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 8596783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:45:43+00:00 2026-06-12T00:45:43+00:00

I am having an odd issue in which the SharedPreferences are not being updated

  • 0

I am having an odd issue in which the SharedPreferences are not being updated upon returning to an app. Here’s the scenario:

I have two projects that use the same shared preferences. Project1 and Project2. They are separate but related apps. They are signed with the same key and use sharedUserId to share information.

Project1 opens Project2.

Project2 retrieves the SharedPreferences file and writes to it via this method:

Context prefsContext = c.createPackageContext(packageNameOfProject1, Context.CONTEXT_IGNORE_SECURITY);
SharedPreferences prefs = prefsContext.getSharedPreferences(fileName, Context.MODE_PRIVATE);
SharedPreferences.editor editor = prefs.edit();
editor.putBool("bool1", value1);
editor.putBool("bool2", value2);
...
editor.putBool("boolN", valueN);
editor.apply();

Once that is done, I return to Project1 by calling finish().

Project1 then reads the data like so:

SharedPreferences prefs = getSharedPreferences(getPreferencesFileName(), Context.MODE_PRIVATE);
Boolean value1 = prefs.getBoolean(fileName, false);
Boolean value2 = prefs.getBoolean(fileName, false);
...
Boolean valueN = prefs.getBoolean(fileName, false);
Map<String, ?> mappings = prefs.getAll();
Set<String> keys = mappings.keySet();
for(String key : keys) {
  log.d(TAG, "_____");
  log.d(TAG, "Key = " + key);
  log.d(TAG, "Value = " + mappings.get(key));
}

The problem is the values are not updated in Project1. I can tell based off the logs at the end that the file isn’t even generating mappings. However, I can verify that the xml is being updated. If I force stop the app then restart it, all the mappings are there in Project1. All the values are correct. However, I need them updated when the user leaves Project2. I feel like there’s something I’m missing here but can not spot it.

The only things I have been able to find on the subject is:

SharedPreferences.Editor not being updated after initial commit

SharedPreferences value is not updated

These don’t help as I’m already doing that.

I have WRITE_EXTERNAL_STORAGE set in both manifests. The fileName is the same (else I wouldn’t be able to read the file when I reenter the app).

EDIT:

I should note that I did try to do editor.commit() instead of editor.apply() as I thought I was facing a race condition. The problem still persisted. I’m thinking that for some reason, the old reference to the SharedPreference in Project1 is being used instead of a new one even though I’m lazy-loading it each time.

EDIT2:

Ok, to further test to see what id going on. I decided to try the opposite direction.

In Project1 I do:

Float testFloat (float) Math.random();
Log.d("TEST_FLOAT", "Project1: TEST_FLOAT = " + testFloat);
prefs.edit().putFloat("TEST_FLOAT", testFloat).commit();

In Project2 I do:

Log.d("TEST_FLOAT", "Project2: TEST_FLOAT = " + prefs.getFloat("TEST_FLOAT", 0.0f));

I then go back and forth between the two like so: Project1->Project2->Project1->Project2->Project1->Project2 and here is the logcat result:

Project1: TEST_FLOAT = 0.30341884
Project2: TEST_FLOAT = 0.30341884
Project1: TEST_FLOAT = 0.89398974
Project2: TEST_FLOAT = 0.30341884
Project1: TEST_FLOAT = 0.81929415
Project2: TEST_FLOAT = 0.30341884

In other words, it’s reading and writing to the same file. However, it’s keeping the mapping that it had when it was first opened it in the project. Even though I close the project, the mapping remains until the application is force stopped.

  • 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-12T00:45:44+00:00Added an answer on June 12, 2026 at 12:45 am

    EDIT: I’m still getting upvotes on this answer even though it recommends a method that has since been deprecated. If you need consistent data through multi-process, then you need to use something other than SharedPreferences like a ContentProvider backed by a file system or database.

    https://developer.android.com/reference/android/content/Context#MODE_MULTI_PROCESS


    Final answer:

    Replace

    getSharedPreferences(fileName, Context.MODE_PRIVATE);

    with

    getSharedPreferences(fileName, Context.MODE_MULTI_PROCESS);

    As per document:

    Context.MODE_MULTI_PROCESS

    SharedPreferences loading flag: when set, the file on disk will be
    checked for modification even if the shared preferences instance is
    already loaded in this process. This behavior is sometimes desired in
    cases where the application has multiple processes, all writing to the
    same SharedPreferences file. Generally there are better forms of
    communication between processes, though.

    This was the legacy (but undocumented) behavior in and before
    Gingerbread (Android 2.3) and this flag is implied when targeting
    such releases. For applications targeting SDK versions greater than
    Android 2.3(Gingerbread), this flag must be explicitly set if desired.

    I knew there was a simple oversight in this.

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

Sidebar

Related Questions

I've been having an issue which is extremely odd to me. The graphic I'm
I'm having an odd issue with CALayer drawing for the iPhone. I have a
I'm having an odd issue in FF10 using jQuery 1.7.1. I have a gallery
I'm having a particularly odd issue when developing Chrome extensions using jQuery. I have
I have having a very odd issue when utilizing this UIAlertView. When viewing a
I have an odd issue that I can not for the life of me
I have got an odd issue in an app I am writing. It reads
I'm having an odd issue where, according to Javascript (in Chrome), all of the
I'm having a rather odd issue where the menu for an activity works totally
I'm having this issue where i use nth-child(odd) to color a table. In IE9

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.