What is proper way to exchange data or objects between different Android Activities?
Welcome screen <-> Main Screen <-> Startup routines <开发者_如何学C-> 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
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).
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.
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.
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)
精彩评论