简体   繁体   English

通过Android中的多个活动传递数据

[英]Passing data through multiple activities in android

I have application in which I have three activities. 我的申请中有三项活动。 First and second activity have listview. 第一个和第二个活动具有列表视图。 I need selected values from the first and second activity in order to display some data in the third activity. 我需要从第一个和第二个活动中选择值,以便在第三个活动中显示一些数据。 Which is the best way to do this? 哪个是最好的方法? Is it better to pass Intent from first to second activity, then from second to third activity or to use SharedPrefenreces? 是将Intent从第一活动传递到第二活动,然后从第二活动传递到第三活动还是使用SharedPrefenreces更好?

This depends on what you need. 这取决于您的需求。 If you need the data to be persistent in the event that your app is destroyed, then you will probably want to save the data in SharedPreferences (will also depend on what type of data since SharedPrefs aren't limited to what they can store) or a DB. 如果您需要在应用程序被破坏的情况下保持数据持久性,那么您可能需要将数据保存在SharedPreferences (这也将取决于哪种数据类型,因为SharedPrefs并不限于它们可以存储的内容),或者一个数据库。

If you aren't worried about it being persistent then passing the data through Intent.putExtra() is fine. 如果您不担心它具有持久性,那么可以通过Intent.putExtra()传递数据。 You can also create a Bundle and pass the Bundle through the Intent and add the data as you go. 您还可以创建一个Bundle,并通过Intent传递Bundle并随需添加数据。

You also have the option of creating a separate class which implements Serializable . 您还可以选择创建一个implements Serializable的单独的类。 Then you can create and pass the class Object through your Intents . 然后,您可以通过Intents创建并传递类Object Here is a post about doing that 这是关于这样做的帖子

Yes this can be achieved in many ways using : 是的,可以使用以下多种方法来实现

  • Intents 意向
  • Shared Preferences 共享首选项
  • Sqlite Sqlite

  1. Sqlite : You can use Sqlite to store data if the data is huge in first and second activity and retrieve the data in the third activity (But if data is less this is not a feasible solution) Sqlite :如果第一个和第二个活动中的数据很大,则可以使用Sqlite来存储数据,而在第三个活动中检索数据(但是如果数据较少,这不是可行的解决方案)

  2. Shared Preferences : Here the data you have saved in the shared-Preferences is done in a xml file, so even if the app is stopped the data is retained. 共享首选项 :在这里,您保存在共享首选项中的数据是在xml文件中完成的,因此即使应用程序停止了,数据也会保留。 This is helpful to save the password and login details etc 这有助于保存密码和登录详细信息等

  3. Intents : My opinion Passing data with intents is much better option in your case because size of the data passed between your first, second to third activity is less. 意图 :我的观点在您的情况下,通过意图传递数据是更好的选择,因为在您的第一,第二至第三次活动之间传递的数据量较小。


Finalizing :: Just put the data in bundle in first activity. Finalizing ::只需将数据放入捆绑包中进行第一个活动。 Next get the data(Bundle) in second activity and add the data in second activity in addition to the data in the bundle received from first activity and pass that final bundle from second activity to the third activity. 接下来,获取第二活动中的数据(捆绑包),并将第二活动中的数据添加到从第一活动中接收到的捆绑包中的数据之外,然后将最终捆绑包从第二活动传递到第三活动中。

In my opinion, it would be easiest to create an ArrayManager class, in which your variables are static. 我认为,最简单的方法是创建一个ArrayManager类,其中的变量是静态的。 This way you can create methods to select and resize your arrays in the ArrayManager class. 这样,您可以创建方法以选择ArrayManager类中的数组并调整其大小。 The only case in which this is bad is if you must hold references to Views, Contexts, or Activities (this will create a memory leak). 唯一不好的情况是您必须持有对视图,上下文或活动的引用(这将导致内存泄漏)。 In that case, codeMagic's solution is ideal. 在这种情况下,codeMagic的解决方案是理想的。

In your case, if you need to pass same data from first activity to second activity then third activity & your data is small then I suggest use Shared Preferences . 在您的情况下,如果您需要将相同的数据从第一个活动传递到第二个活动,则第三个活动和您的数据很小,那么我建议您使用Shared Preferences

If data passed from first activity to second activity is not same data of second activity to third activity then simply use intent. 如果从第一活动传递到第二活动的数据与第二活动到第三活动的数据不同,则只需使用意图。

There is a tricks. 有一个把戏。 If your second activity always need data from first activity to operate then when you want to go back third activity to second activity then you will get stuck because from third activity you can not get first activity result. 如果您的第二个活动始终需要第一个活动的数据才能运行,那么当您想将第三个活动返回到第二个活动时,您将陷入困境,因为从第三个活动中您将无法获得第一个活动的结果。 In that case saving data i mean Shared Preferences or Sqlite is best. 在那种情况下,保存数据意味着Shared PreferencesSqlite是最好的。

All above those scenario you need to find your own what is best to use for your app. 在上述所有情况下,您都需要找到自己的最佳应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM