开发者

Dealing with shared preferences stored in a library

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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜