本文介绍了在Android TabHost应用程序中完成活动间通信的最佳方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

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

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:

((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.

推荐答案

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

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.

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

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

任何Activity都可以调用getApplication(),它将返回自定义Application类的实例,然后你可以根据自己的口味来定制。

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

这篇关于在Android TabHost应用程序中完成活动间通信的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 14:20