如何从选项卡之一访问包含选项卡主机的主要活动中的进度栏?
我正在开发一个 Android 应用程序,其中有一个包含进度条和 tabhost 的主要活动。 tabhost 有 3 个选项卡。
如何从选项卡活动访问主活动中的进度栏?我希望能够在每个选项卡活动内发生变化时启动和停止进度条......
I am developing an android application where I have a main-activity that contains a progress bar and a tabhost. the tabhost has 3 tabs.
How do I from a tab-activity access the progress bar in the main activity? I want to be able to start and stop the progress bar when things changes inside each tab ativity...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在每个选项卡-Activity 中使用 getParent()
use getParent() in each tab-Activity
几种方法:
将类耦合在一起:创建自定义应用程序并将对 ProgressBar 的弱引用保存到字段中。在选项卡活动中使用
getApplication()
并将其转换为您的自定义应用程序。注意:在 Application 类中使用弱引用来防止内存泄漏 .
创建您自己的
Broadcast
和BroadcastReceiver
,其中选项卡活动发送广播并且进度栏侦听它们:http://developer.android.com/guide/appendix/faq/commontasks.html#broadcastreceivers阅读如何在 Activity 之间传递数据。
Several ways:
Couple the classes together: create a custom Application and save a weak reference to ProgressBar to a field. Use
getApplication()
inside tab activities and cast it to your custom Application.Note: Use weak reference inside your Application class to prevent memory leaks.
Create your own
Broadcast
andBroadcastReceiver
, where tab activities send broadcasts and progress bar listens to them: http://developer.android.com/guide/appendix/faq/commontasks.html#broadcastreceiversRead how to pass data between Activities.