views:

70

answers:

1

For example, I want my users to be able to click a button "Get new quote" which will start a new row in the the database for data to start being stored, but I'm unsure of how to store activity independent (or cross activity) data, like the _id of the row, which I will need to requery and update the row when new data becomes available on subsequent activities. What's the name of this mechanism and how is it done?

Any ideas or guidance is appreciated. Thanks.

+2  A: 

How do I pass data between Activities/Services within a single application?

It depends on the type of data that you want to share:

Primitive Data Types To share primitive data between Activities/Services in an application, use Intent.putExtras(). For passing primitive data that needs to persist use the Preferences storage mechanism.

Non-Persistent Objects For sharing complex non-persistent user-defined objects for short duration, the following approaches are recommended:

The android.app.Application class

The android.app.Application is a base class for those who need to maintain global application state. It can be accessed via getApplication() from any Activity or Service. It has a couple of life-cycle methods and will be instantiated by Android automatically if your register it in AndroidManifest.xml.

A public static field/method

An alternate way to make data accessible across Activities/Services is to use public static fields and/or methods. You can access these static fields from any other class in your application. To share an object, the activity which creates your object sets a static field to point to this object and any other activity that wants to use this object just accesses this static field.

A HashMap of WeakReferences to Objects

You can also use a HashMap of WeakReferences to Objects with Long keys. When an activity wants to pass an object to another activity, it simply puts the object in the map and sends the key (which is a unique Long based on a counter or time stamp) to the recipient activity via intent extras. The recipient activity retrieves the object using this key.

A Singleton class

There are advantages to using a static Singleton, such as you can refer to them without casting getApplication() to an application-specific class, or going to the trouble of hanging an interface on all your Application subclasses so that your various modules can refer to that interface instead.

But, the life cycle of a static is not well under your control; so to abide by the life-cycle model, the application class should initiate and tear down these static objects in the onCreate() and onTerminate() methods of the Application Class

Persistent Objects Even while an application appears to continue running, the system may choose to kill its process and restart it later. If you have data that you need to persist from one activity invocation to the next, you need to represent that data as state that gets saved by an activity when it is informed that it might go away.

For sharing complex persistent user-defined objects, the following approaches are recommended:

Application Preferences
Files
contentProviders
SQLite DB

If the shared data needs to be retained across points where the application process can be killed, then place that data in persistent storage like Application Preferences, SQLite DB, Files or ContentProviders. Please refer to the Data Storage for further details on how to use these components.

Pentium10
Well, that would work if the app just had the phone owner as the user, but in my case, I want the user to be able to create multiple users, and therefore there will be multiple rows of data.I'm storing everything in a SQLite db, which works fine, but when I resume a user, I want to pull the ID out and pass it from activity to activity. So far, passing an extra in the intent is the only thing that works, just not sure if this is correct or the most efficient way.
Allen Gingrich
OK I updated my answer, your way to go is via `android.app.Application class`
Pentium10
I'm leaving the office now, so haven't the time to check what works or not, but that is one concise answer!Thanks so much. I shouldn't have any problems finding how to share data now.
Allen Gingrich