且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在 Android 选项卡之间进行通信

更新时间:2023-01-28 18:32:19

肯定想重新考虑使用活动作为选项卡的内容.更标准的方法是使用一个使用选项卡的 Activity,仅在选择特定选项卡时显示部分布局.

You definitely want to reconsider using Activities as the content of your tabs. The more standard approach is to use one Activity that uses Tabs to only show part of the layout when a particular tab is selected.

Android 文档有一个很好的示例,请查看 你好,TabWidget.

The Android documentation has an excellent worked example, check out Hello, TabWidget.

替代方案

如果出于某种原因您确实需要使用活动,您可以通过将值添加到用于打开每个活动的 Intent 中的附加包,或通过扩展 Application 类.

If for some reason you do need to use Activities, you can pass information between them by either adding values to the extras bundle within the Intent your using to open each Activity, or by extending the Application class.

通过扩展 Application 类(并将其实现为 Singleton),您将获得一个在任何应用程序组件存在时都会存在的对象,从而提供一个集中的位置来存储和在应用程序组件之间传输复杂的对象数据.

By extending the Application class (and implementing it as a Singleton) you get an object that will exist whenever any of your application components exist, providing a centralized place to store and transfer complex object data between application components.