Android sharing data (state,value,config) between multiple apps & activities
Hi We are developing a complex application which involves multiple screens. We decided to keep the common activities in a separate applications apk ( commonScreen apk), so that these can be used by ourselves as well as others using intents.
Now, we need a common way to store and share the data between the activities present in our main application and commonScreen apk.
We thought of storing data in Application object ( extend Application) But, when we call startActivityForResult(commonScreenApkIntent), onActivityResult, we can see that the data present in Application is gone (null) see Dont store global data in application for issues using them.
Is storing and retrieving the data in each activity using preference is a good method when large data is involved?
Please note : the amount of data we want to store are more, which are basically application buttons 开发者_如何学Gostate ( disabled/enabled), value and configurations. It might go up to 1000 values.
Can you please suggest a way to share the data?
Have you considered using SQLite ?, ContentProviders can make your life easier
I would strongly encourage you to reconsider this design of your app. Code in separate .apks should generally be able to run independently, not have close couplings with data. If this is all one app and it is split into .apks for some kind of convenience, you are going to cause yourself all kinds of other problems -- for example, what if the user uninstalls one of the .apks, clears the data on one of them, etc...?
If you are thinking about using MODE_WORLD_READABLE, stop and reconsider. First not that this is allowing any application installed on the device to read that data. It also generally indicates a design issue with a bad dependency between apps that will bite you.
I agree with what hackbod said.
(Dhamodharan is just promoting his website.)
Your Question “Is storing and retrieving the data in each activity using preference is a good method when large data is involved?”
My answer= No. Preference should be used for small primitive data (ie= The grade each student has in your class).
SharedPreferences stores the result in an XML file located within the App. If u want to find the XML 1) Run ur App. 2) go to DDMS 3) Using “File Explorer”, go into the directory Data-> data -> your package name (com.example.whatever) -> shared_prefs. There u should see an xml document.
Your storage options http://developer.android.com/guide/topics/data/data-storage.html:
Shared Preferences= Store private primitive data in key-value pairs.
Internal Storage= Store private data on the device memory.
External Storage= Store public data on the shared external storage.
SQLite Databases= Store structured data in a private database.
Network Connection= Store data on the web with your own network server.
I suggest u store your data in using SQLite. That way u can use Google’s massive datacenter. If you have your own server, then use Network Connection. External Storage would be my 3rd suggestion because external memory (SD cards) tend to have a lot more space than internal memory (Internal Memory= Internal Storage or Shared Preferences), especially on low-end phones. For example, I have a Samsung Gio. The internal memory is 181MB. The SD card holds almost 2 GB. BIG Difference!
If you’re doing all this because you have “multiple screens” you should be looking into fragments. Fragments is what allows u to control one Application on multiple screen sizes (ie= phone and tablets).
It sounds like u have it all backwards. The common Activity should have the power to control what Intents are called, not the other way around. You should have a common Activity that controls which Fragments show up based on the device. Then inside each individual Fragment Object, u can use SQLite to retrieve saved states.
精彩评论