2

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

Dima Kozhevin
  • 3,602
  • 9
  • 39
  • 52

6 Answers6

8

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

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

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
0

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

Kartik Shandilya
  • 3,796
  • 5
  • 24
  • 42
  • But this issue doesn't occur if the min sdk is set to 8 in manifest of app1. – Shivani srivastava Oct 10 '17 at 16:33
  • Are you sure with it? The Shared Preferences came in API Level 1 and this isn't mentioned in docs as well. – Kartik Shandilya Oct 10 '17 at 16:35
  • Yes there is no documentation related to this. But I am facing this issue. Not sure about the relation of SharedPreferences with min sdk. It works properly fine if I getsharedpreferences in multi_mode processes. But I want to use mode_private – Shivani srivastava Oct 10 '17 at 16:40
0

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.

Nabin Bhandari
  • 15,949
  • 6
  • 45
  • 59
0

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

Uddhav P. Gautam
  • 7,362
  • 3
  • 47
  • 64
  • I don't know yet. I don't think it is possible. Because when a process goes in critical section it locks its memory space. After it gets killed it flushes that data to SharedPreferences. Now, another process can get the updated value. I am also researching on it, will let you know if I know... – Uddhav P. Gautam Oct 10 '17 at 17:01
0

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.

0

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.

m02ph3u5
  • 3,022
  • 7
  • 38
  • 51