0

I want know i pass a variable via Intent to another activity and that activity changes that variable, will it reflect in original activity without passing back the intent. If answer is no then is it better to use global variable using application then passing intent and getting back data. in my program, i am having round 5+ activities and all of them need to access a list of class objects. any recommendations apart from above

4

1 回答 1

0

创建您自己的扩展Application来存储您的应用程序的状态并在组成您的应用程序的不同活动之间共享数据。Application充当整个应用程序的上下文,Android 保证在您的应用程序中始终只有一个实例。因此,它的工作方式类似于定义您自己的 Singleton,但使用Application将允许 Android 控制您共享数据的生命周期并基本上为您进行内存管理。

总结一下:

  1. 创建您自己的Application.
  2. 在清单的应用程序标记中指定该类。

在此之后,您将能够安全地将所有调用getApplication()(从Activity实例)和getApplicationContext()(从任何Context实例)的结果转换为您在步骤#1 中定义的子类。这意味着您可以使用应用程序扩展中定义的任何 getter/setter 方法来存储/检索数据。

阅读此处了解更多详情

于 2012-09-06T02:50:11.573 回答