views:

93

answers:

1

I have an app that has a free, and a paid version. I put all the free version code into a library, which I reference from a new free version project, and the paid version project.

I noticed that the library, and now my projects all have a properties file, and my preferences stopped working completely. It seems that depending on what namespace the code executes in, it gets a different result when you query for SharedPreferences.

What is the best way to deal with this? Both apps have the same preferences as of now, but I will probably extend the paid version later and add preferences that don't exist in the free version.

+1  A: 

I assume you are asking whether your library can share prefs with the top level app, not whether the free version can share with the full version.

The api docs mention that you can't share prefs outside of a given process, so is it possible that since you (probably) have two different packages (one for lib, one for top-level), the prefs you get in the library vs the top level app are from different files?

You can check this by browsing your phone storage in /data/data/your-package for the preference file for the top level app and the library. You might find that you have a different preference file for each, which I think means that you can't share prefs between the library / app.

You might be able to get around these limitations by only reading and writing to prefs from the top level app, and creating an interface to deliver those prefs to the library. If your library is simply presented with the values from the top level, then the library won't have to access the prefs, and thus all your preference access is done from a single place.

Also, this will allow you to have different preferences for full vs. free (which is the default behavior anyway, since they will be stored in separate preference files), and then you can send the proper info down to the lib based on version.

Josh
You are right, there are two files for preferences. One in the library, and one in the app. To read/write the library preferences I created a wrapper class in the library around the SharedPreferences returned from the PreferenceManager.getDefaultSharedPreferences. To read/write any preferences specific to the app, I use the SharedPreferences returned from a call to the PreferenceManager.getDefaultSharedPreferences in a non-library class.
Scienceprodigy