如何在不同的Android活动之间交换数据(对象)?

Ant*_*nte 6 android

在不同的Android活动之间交换数据或对象的正确方法是什么?

欢迎屏幕< - >主屏幕< - >启动例程< - >处理数据< - >设置

在Android应用中有多个活动是正常/建议的吗?在我看来,在应用程序中使用这种数据交换模型有些奇怪

Com*_*are 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).

你如何处理Android应用程序?好吧,您将模型保存在中心位置(数据库ContentProvider等),并Intent在主要UI(活动)单元之间的链接中传递一小部分上下文数据(附加内容).


Rof*_*ion 6

Android开发者页面上有一个概述:

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

总结一下,它取决于您要传递的数据类型.但一般来说,如果可能的话,我会选择Intents,因为它们很快并且为此而构建.

下面是一个示例,说明如何使用Intents在活动之间传递数据.

  • 只使用"Intent"附加内容来处理与你正在启动的内容相关的内容.可以将它们视为与网站上页面之间的链接中使用的URL上的参数等效.特别是,不要将模型数据存储在"Intent"附加内容中,因为当您将数据从活动传递给活动时,最终会获得N个副本.此外,"Intents"在内存中保留了相当长的一段时间,有时甚至在活动被破坏之后,因此内存成本变得更高. (4认同)