gaurav jain
gaurav jain

Reputation: 3234

Shared Preferences deleted automatically

I'm developing an application in which I'm using a background service, and I save my data to shared preferences (as I know Android can kill services whenever there's low memory state). It works fine, and I periodically save my data to preferences. But as a rare scenario, I noticed as Android killed my service and restarted it (there was a gap of around 20-30 seconds between this), in this interval my preferences got automatically cleared. I checked up the dumpstate to infer the reason behind this, but I cannot understand why this happens.

Here's the relevant part of logs, which indicate data clearance from preferences:

    06-25 17:30:34.848  2452  2966 V ApplicationPolicy: isApplicationStateBlocked userId 0 pkgname com.example.gaurav
06-25 17:30:34.863  2452  2966 D EnterpriseDeviceManager: ContainerId: 0
06-25 17:30:34.863  2452  2966 V ApplicationPolicy: isApplicationStateBlocked userId 0 pkgname com.example.gaurav
06-25 17:30:34.863  2452  2966 D PackageManager: START CLEAR APPLICATION USER DATA: observer{1126181288}
06-25 17:30:34.863  2452  2966 D PackageManager: pkg{<packageName>}
06-25 17:30:34.863  2452  2966 D PackageManager: user{0}
06-25 17:30:34.898  2831  2831 D ContainerEventsRelayManager: <<< Intent action is >>> : android.intent.action.PACKAGE_RESTARTED
06-25 17:30:34.898  2831  2831 D ContainerEventsRelayManager: <<< Intent data is   >>> : package:com.example.gaurav
06-25 17:30:34.898  2695  2695 D ContexualWidgetMonitor:  id = 0
06-25 17:30:34.898  2695  2695 D ContexualWidgetMonitor: action =android.intent.action.PACKAGE_RESTARTED
06-25 17:30:34.898  2695  2695 D ContexualWidgetMonitor: pkg =null
06-25 17:30:34.898  2695  2695 D ContexualWidgetMonitor: mCheckMissedEvent =false
06-25 17:30:34.903  2831  2831 D ContainerEventsRelayManager: <<< Intent action is >>> : android.intent.action.PACKAGE_DATA_CLEARED
06-25 17:30:34.903  2831  2831 D ContainerEventsRelayManager: <<< Intent data is   >>> : package:com.example.gaurav
06-25 17:30:34.908  2452  2580 I PackageManager: remove MCS_UNBIND message and Posting MCS_UNBIND 10 secs later

Though this scenario didn't occur again, but I'd really like to know what's happening behind the scenes, and why does it happen. I've searched other similar threads here, but they indicate the scenario where null key is inserted into preferences, so I guess this is entirely different.

Any help would be appreciated. Also, as an information, this problem occurred on Samsung Galaxy Note 2 device.

Upvotes: 3

Views: 5217

Answers (1)

luksfarris
luksfarris

Reputation: 1551

this happens because you are writing the Preferences in a background thread. It is a known issue that when two threads access the same preferences file, the preferences get deleted. You should not use SharedPreferences with processes, instead use a Database.

Here`s another SO question with the same problem.

I am going through the exact same problem, and I'm going for SQLite.

Upvotes: 3

Related Questions