# Help with SharedPreferences and Process specifiers..



## Foxtrot_Xray (Dec 28, 2011)

Hey guys.. Need some expert help, and .. well, ya'll're the experts. 

In short: In my app, I have to specify two different process identifiers for two activities in the manifest, because of Google Maps API. The other activity, PreferenceActivity for setting app preferences and such, has no "androidrocess=" moniker. This introduced an interesting problem that took me a while to solve, whereas the main activity would launch the PreferenceActivity fine. However, on returning from the PreferenceActivity, the main activity would still contain the original settings.

For example:
Setting: com.tsqmadness.bmmaps.showMapSatellite = true on program run.
Main activity sees 'true', sets map settings.
User hits menu and preferences, and changes 'Show Map Satellite' to false.
User hits Back.
Main activity gets new instance of shared preferences and gets the value, value is still 'true', until app is exited and relaunched.

I'm getting a new instance to the SharedPreferences, so that's not the issue. As soon as I add 'androidrocess=""' in the manifest, and match it to the process of the main activity, the setting is read properly. Which is an easy fix.

However.. The other activity, the one with the different process, which requires a different process for the Maps API, will still read the value that was set at app launch, not after it's been changed.

Am I missing something? Is there a way to get the differently-named process activity to read the changed values, by forcing a reload, or some such?

Thanks guys -
-Mike.

(Edit: A little backroung is that I'm not an android programmer, and not a Java programmer. So.. I could be missing something really simple.  )


----------



## JBirdVegas (Jun 11, 2011)

try adding in your main class a SharedPreferences Listener to watch of changes in specific values and run some updateUI() or whatever you do to refresh

also the calls to the shared preferences are batched so be sure you commit()

EDIT: I just realized what your problem is... SharedPreferences has a problem it's not synchronous across processes (there is an official bug report somewhere and they say in the docs it's on their todo) for now try adding a listener but it may require some hackery to get the info across process

ie you could return the info from your preferences (via startActivityForResult(intent, int)) and save the preferences in the main thread if any info was returned or changed from what is already stored, avoiding the async problem all together or see the next post


----------



## JBirdVegas (Jun 11, 2011)

if push really came to shove you could put your prefs in a config file via Properties then when you come back from preferences just check then config ,or FileObserver in the main thread but that's more of a work around :-/


----------



## Foxtrot_Xray (Dec 28, 2011)

JBirdVegas said:


> if push really came to shove you could put your prefs in a config file via Properties then when you come back from preferences just check then config ,or FileObserver in the main thread but that's more of a work around :-/


I would, if I had to worry about more than one preference. Since I can put the SharedPreferences and the main Activity on the same named process, that helps in all cases except for the other side activity, which only grabs a single preference. 

Thanks again -
Mike.


----------



## JBirdVegas (Jun 11, 2011)

Well I'm glad it was a simple answer let us know if you find some new roadblocks


----------



## Foxtrot_Xray (Dec 28, 2011)

Will do, many thanks!

I think my next post will probably be about icons/images for a theme for the app, as I'm completely inept at that.


----------

