Shivani srivastava
Shivani srivastava

Reputation: 47

SharedPreferences in Android not getting recently updated values across multiple running processes

Description: I have two apps App1 and App2. I am using App1 to store some key value pair in SharedPreferences. I am accessing the same key value in App2.

I launch app1. Create a key with value abc. Now I keep app1 in background and launch App2 and I change the key value to def.

When I launch app1 from background to foreground and access the key value. Value retrieved is abc instead of updated value def. If I kill App2 from background and relaunch it then only updated value is getting reflected

Upvotes: 2

Views: 250

Answers (6)

Shivani srivastava
Shivani srivastava

Reputation: 47

SharedPreferences has never supported multiple processes, let alone multiple apps. The documentation explicitly states:

Note: This class does not support use across multiple processes.

SharedPreferences had to be replaced with content provider in my case.

Upvotes: 0

Zeerak Hameem
Zeerak Hameem

Reputation: 147

Sharedpreferences are not process safe as per their documentation. I ran into a similar problem. I was using a value from sharedpreferences within an activity and then in a service with another process. The same value was behaving like two different values.

Well i used this lib to solve this problem. Its basically the same approach and syntax and its process safe.

Upvotes: 0

Uddhav P. Gautam
Uddhav P. Gautam

Reputation: 7636

When process reads the data from the SharedPreferences, it copies the values (data) and puts those data inside it's process inside the cache. It does this because reading/writing from disk is too slow. Generally, System creates cache process for Android Components. Now, your data from the SharedPreferences is stored in the component's cache process. Cache process has it's own memory space. Until the component is using the memory, it locks (critical section) in order to prevent other can't access that memory. The cache process should be killed so that other processes (or components) can now be able to access it. This is why when you relaunch you see the update.

Critical Section: https://en.wikipedia.org/wiki/Critical_section

Different Processes: https://developer.android.com/guide/components/activities/process-lifecycle.html

In thread-level, you use the keyword volatile, so that you see the one thread updated data from second thread. But SharedPreferences can't be volatile because it is persistent storage.

That's why they explicitly stated SharedPreferences doesn't support on multiple processes

Upvotes: 0

Nabin Bhandari
Nabin Bhandari

Reputation: 16409

The shared preferences of an app are stored in a separate file within the app's internal storage which no other apps can access. The shared preferences of two different apps are located in two different locations and are independent of each other.

So the shared preferences of an app cannot be accessed by any other apps and change in the shared preferences of an app is not reflected in any other apps.

Upvotes: 0

Kartik Shandilya
Kartik Shandilya

Reputation: 3924

The value doesn't get updated as the SharedPreferences doesn't recognize data change across multiple processes.

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1007276

SharedPreferences has never supported multiple processes, let alone multiple apps. The documentation explicitly states:

Note: This class does not support use across multiple processes.

Upvotes: 8

Related Questions