Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个有几个屏幕的应用程序。假设 ABCD 和 D 也可能会打开一些外部应用程序。
所有这些活动都共享数据,一个数组列表,我在我的应用程序类中创建了一个对它的引用。(我创建了一个扩展 Application 的类并引用它清单。)所以所有这些都使用 arraylist 的单个实例。A 从第一个屏幕开始初始化数组列表,其他人可能会修改它。
问题是当我在模拟器上测试它时,没有任何东西被破坏。但是在“某些”手机上,经过 3 个以上的导航 arraylist 屏幕后,只会从堆中清除。不管尺寸有多小。
使用单例设计模式或使您的对象静态
使用单例设计模式,您可以:
为您的 ArrayList使用一个singleton类
singleton
ArrayList像static你的第一个一样声明,Activity然后从它不会分解的任何地方使用它。
ArrayList
static
Activity