java 在 Android TabHost 应用程序中完成交互活动通信的最佳方式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1162659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 15:26:49  来源:igfitidea点击:

Best way to accomplish inter-activity communication in an Android TabHost application

javaandroidipc

提问by MattC

Here's the deal: I have an Android application that needs to call a web service every X seconds (currently 60 seconds). This application has multiple tabs and these tabs all need to interact with the data themselves. One is a MapView, one is a ListView and then the third is irrelevant but will need to also get some global data eventually. The issue is that I want my main activity to have a thread that runs in the background, gets the results and then instructs both child activities in the TabHost to update themselves with the latest data. Also, when the user clicks on the tabs and the onCreate/onResume activities fire, I would also like to force a redraw by getting the latest data from the main activity. I'm really at a loss here. I've tried this with a service and some ghetto static methods to pass an instance of the Activities to the Service to call specific functions to update their views whenever the timer fired, but the slowdowns were pretty bad and the code was just ugly ugly ugly. Any suggestions?

交易如下:我有一个 Android 应用程序,它需要每 X 秒(当前为 60 秒)调用一次 Web 服务。这个应用程序有多个选项卡,这些选项卡都需要与数据本身进行交互。一个是 MapView,一个是 ListView,然后第三个是无关紧要的,但最终还需要获取一些全局数据。问题是我希望我的主要活动有一个在后台运行的线程,获取结果,然后指示 TabHost 中的两个子活动使用最新数据更新自己。此外,当用户单击选项卡并触发 onCreate/onResume 活动时,我还想通过从主要活动中获取最新数据来强制重绘。我真的很茫然。一世' 我已经尝试过使用服务和一些 ghetto 静态方法将活动的实例传递给服务,以在计时器触发时调用特定的函数来更新它们的视图,但是速度很慢,而且代码非常丑陋。有什么建议?

edit: So I implemented it as a timer-driven thread in the tabhost activity and then I have timer-driven threads in each child activity that then grab the data (in a synchronized fashion) and update their map/list. It's much faster but still feels slightly hack-ish, especially the part where I'm calling a custom function in the parent activity like so:

编辑:所以我将它实现为 tabhost 活动中的计时器驱动线程,然后我在每个子活动中都有计时器驱动的线程,然后获取数据(以同步方式)并更新它们的地图/列表。它更快,但仍然感觉有点hack-ish,尤其是我在父活动中调用自定义函数的部分,如下所示:

((MainActivity)getParent()).getNearbyMatches();

This adds an element of strong coupling that I'm not entirely thrilled with, but from a performance standpoint it's much better than it was. I appreciate the answers that have already been given and will do a bit of research on the content provider front but I'm not sure I want to go back to the service model.

这增加了一个强耦合元素,我并不完全感到兴奋,但从性能的角度来看,它比以前好得多。我很欣赏已经给出的答案,并将在内容提供商方面做一些研究,但我不确定我是否想回到服务模型。

回答by MattC

So I've found what I believe is the answer: The Application Class. You can extend this class to keep track of global application state.

所以我找到了我认为的答案:应用程序类。您可以扩展此类以跟踪全局应用程序状态。

In the AndroidManifest.xmlfile you can reference your fully qualified custom class in the android:nameattribute and it will be instantiated when the app fires up.

AndroidManifest.xml文件中,您可以在android:name属性中引用完全限定的自定义类,它会在应用程序启动时实例化。

Any Activity can then call "getApplication()"and it will return the instance of your custom Application class, which you can then tailor to taste.

然后任何 Activity 都可以调用"getApplication()",它会返回您的自定义 Application 类的实例,然后您可以根据喜好进行定制。

回答by Prashast

Why are you updating all the children activities every time new data is available? That sounds inefficient to me. Update only the activity that is currently visible.

为什么每次有新数据可用时都更新所有儿童活动?这对我来说听起来效率低下。仅更新当前可见的活动。

One possible way to do this is through a custom content provider. Let your service update the data source to your activities and get the current visible activity to listen to changes on this content. So basically, your register to the content provider when OnResume is called and unregister when OnPause is called.

一种可能的方法是通过自定义内容提供程序。让您的服务将数据源更新到您的 Activity 并获取当前可见的 Activity 以侦听此内容的更改。所以基本上,您在调用 OnResume 时注册到内容提供者,并在调用 OnPause 时取消注册。

As a rule of thumb never store static references of an Activity!!You'll end up with ugly leaks. If it is a must for your application then at least use WeakReferences

根据经验,永远不要存储活动的静态引用!!你最终会出现丑陋的泄漏。如果您的应用程序必须使用它,那么至少使用WeakReferences

回答by Josef Pfleger

You can implement your GUI updates in Handlers and register the Handlerinstances with your download-thread. The download thread then sends messages to the handlers when new data arrives. Essentially this is the Observer Pattern. You can find an example of how to use Handlers here(expand the 'Example ProgressDialog with a second thread' section).

您可以在Handlers 中实现您的 GUI 更新并Handler使用您的下载线程注册实例。当新数据到达时,下载线程然后向处理程序发送消息。本质上这是观察者模式。您可以在此处找到有关如何使用Handlers的示例(展开“带有第二个线程的示例 ProgressDialog”部分)。