0

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 state ( disabled/enabled), value and configurations. It might go up to 1000 values.

Can you please suggest a way to share the data?

Programmer Bruce
  • 64,977
  • 7
  • 99
  • 97
Ganesh Bhat
  • 687
  • 5
  • 5
  • "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." -- please don't. – CommonsWare Feb 21 '11 at 12:27

3 Answers3

3

Have you considered using SQLite ?, ContentProviders can make your life easier

Reno
  • 33,594
  • 11
  • 89
  • 102
  • Do you mean, I shall be storing the app buttons state, values and configuration I received from webservice in the SQL Lite? Wont it be very heavy on the app? is this the common scenario where I can use content provider? Please suggest. – Ganesh Bhat Feb 20 '11 at 04:19
  • If you meant "thousands of values" then yes use SQL. If you meant "one thousand values", use a key-value arraylist which can be passed using intents or retrieved by using a common data storage class – Reno Feb 20 '11 at 04:26
  • 1
    +1, my immediate thought as I read the question was to suggest using a SQLite DB. – Squonk Feb 20 '11 at 05:45
  • Thanks for the inputs, please suggest me on the approach suggested bellow as a second answer. What i am interested in is consistency, performance and best practice. – Ganesh Bhat Feb 20 '11 at 06:47
  • Hi, you can consider that the amount of values wont exceed 1500 values – Ganesh Bhat Feb 20 '11 at 10:33
1

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.

hackbod
  • 90,665
  • 16
  • 140
  • 154
0

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.

Gene
  • 10,819
  • 1
  • 66
  • 58