android - Android 中正确的交互 Activity 通信?

标签 android android-activity

我想在单个进程中的 Activity 之间共享数兆字节的数据结构和其他对象(指的是所述数据结构)(Parcelable 是不可能的;大部分数据都存在无论如何在 C++ 中)。我不仅要向新 Activity 发送任意对象,还要返回任意结果对象。

但是Activity通过Intents相互启动并返回结果,而Intents似乎没有办法包含任意Object。目前我正在使用 static 变量作为解决方法,但它让我担心内存泄漏,虽然只有一个应用程序实例打算运行(目前),但我不寒而栗地想什么会如果同一个 Activity 的两个实例以某种方式进入 Activity 堆栈,就会发生这种情况。

那么,SO 社区,您会推荐哪种沟通机制?

附言有谁知道 Android 是否曾经生成一个进程的多个副本,例如当两个应用程序独立启动第三个应用程序时?

最佳答案

I would like to share multi-megabyte data structures and other objects (that refer to said data structures) between Activities in a single process

我希望您真正想要的是一个您所有 Activity 都可以引用的集中式数据模型。

I want to not only send arbitrary Objects to new activities, but also return arbitrary result Objects.

再次强调,我希望您真正想要的是一个您的所有 Activity 都可以引用的集中式数据模型。说您想在 Activity 之间传递“数兆字节的数据结构”类似于说您想通过 Web 应用程序中 URL 上的查询参数传递“数兆字节的数据结构”。

Currently I am using static variables as a workaround, but it leaves me concerned about memory leaks, and although only one instance of the app is intended to run (for now), I shudder to think about what would happen if two instances of the same Activity somehow get on the Activity stack.

好吧,如果您实际上拥有一个集中式数据模型(无论是通过静态数据成员还是其他方式),您就不必担心数据复制。此外,每次您通过 Intent extras 传递数据时,您就已经在制作副本了。

So, SO community, what communication mechanism would you recommend instead?

拥有集中式数据模型并在 Intent extras 中传递标识符。

does anyone know if Android ever spawns multiple copies of a process, e.g. when two apps independently start a third app?

默认情况下,每个应用都在一个进程中运行,无论是谁“启动”它们或它们“启动”了多少次。

关于android - Android 中正确的交互 Activity 通信?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11266506/

相关文章:

android - 如何发送邮件到指定邮箱?

android - 禁用 OrientationEventListener 时调用 onOrientationChanged()

android - 选择 Release模式时应用程序崩溃但在 Debug模式下运行完美

Android O 预览版 : ConnectivityManager removed methods are not backward compatible

android - GestureDetector.onTouchEvent(MotionEvent e) 在所有手势上调用 onLongPress

Android:数据库导出适用于模拟器,不适用于手机

android - 调用其他 Activity 方法时显示 NullPointerException

android - 在新方法中创建 Intent

点击按钮时的 Android Loader vs AsyncTask

Android 编译不同资源(白标)