6

What is proper way to exchange data or objects between different Android Activities?

Welcome screen <-> Main Screen <-> Startup routines <-> Processing data <-> Settings

Is it normal/recommended to have more than one activity in Android app? in my opinion, it's somehow strange to have this model of data exchange inside application

Ante
  • 8,567
  • 17
  • 58
  • 70
  • possible duplicate of [How do I pass data between activities in Android?](http://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android) – Bo Persson Oct 30 '11 at 17:05

4 Answers4

9

Is it normal/recommended to have more than one activity in Android app?

Normal? Yes. Recommended? That depends on the app.

in my opinion, it's somehow strange to have this model of data exchange inside application

What do you do with Web apps? Well, you keep your model in a central spot (server) and you pass small bits of context data (URL parameters) in links between major units of UI (pages).

What do you do with desktop apps? Well, you keep your model in a central spot (database) and you pass small bits of context data (e.g., constructor parameters) in links between major units of UI (windows).

What do you do with Android apps? Well, you keep your model in a central spot (database, ContentProvider, etc.) and you pass small bits of context data (Intent extras) in links between major units of UI (activities).

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
6

There is an overview on the Android developer page:

http://developer.android.com/guide/appendix/faq/framework.html#3

To summarize it, it depends on what type of data your going to pass. But generally speaking, if possible, I would choose Intents, because they are fast and built for that.

Here is an example that explains how to pass data between activities using Intents.

RoflcoptrException
  • 51,941
  • 35
  • 152
  • 200
  • 4
    Only use `Intent` extras for things that are tied to what you are starting up. Think of them as equivalent to parameters on a URL used in a link between pages on a Web site. In particular, do not store model data in `Intent` extras, as you wind up with N copies of that data as you pass it from activity to activity. Furthermore, `Intents` stay around in memory for quite some time, sometimes even after the activity is destroyed, so the memory cost becomes that much higher. – CommonsWare Jul 29 '10 at 23:24
4

The answer is: It Depends.

Depending on the architecture of your application, you may want to:

  • Store your data in a custom ContentProvider, and pass around URI references to it --- if your application is based around a database, this is the way to go, as it allows other applications to refer directly to your data items;

  • Have your Activities communicate by sending each other Intents, with the data packaged inside custom Intent data fields --- if you're only using very small items of data, such as names or URIs, this is a simple way of managing things, but it breaks down for larger items;

  • Have all your Activities running inside a single process, and store your data in shared Java objects --- generally not recommended, but suitable for specialised applications such as games (but bear in mind issues to do with application lifecycle!).

My apps tend to use a combination of the first two: the data primarily lives in a ContentProvider, but I also use intents to send out-of-band information between the ContentProvider and the activities when such data won't easily fit in the ContentProvider API.

David Given
  • 13,277
  • 9
  • 76
  • 123
0

Do what google commands you to do here: http://developer.android.com/resources/faq/framework.html#3

Choices: Primitive Data Types Non-Persistent Objects Singleton class - my favorite :D A public static field/method A HashMap of WeakReferences to Objects Persistent Objects ( Application Preferences, Files,contentProviders,SQLite DB)