TabHost:如果我需要处理onOptionsItemSelected每个单独的标签/活动是什么 [英] TabHost: What if I need to handle onOptionsItemSelected each in separate Tab/Activity
问题描述
我需要有一个 TabHost
包含两个标签
,其中每个设置页
再由一个单独的活动psented $ p $
。每个活动
都有自己的数据域和方法对它们进行操作。我想每个标签有自己的
菜单
并为此菜单
我需要 onOptionsItemSelected()
方法是类
,使其能够调用这个方法类
。
什么是最好的做法会是实现这一点?
我已经开始与菜单
定义中重新presents TabHost
类,但随后面对有一个问题,我没有我的活动
的方法的访问。我决定让活动的方法
静态
来访问他们,但最后我需要定义的几乎所有领域这些活动
静态这似乎并没有对我来说是很好的解决方案。
更新
它成为后来觉得要容易得多。在onOptionsItemSelected()就可以得到当前的活动
您打开标签
。
SecondActivity SA =(SecondActivity)this.getCurrentActivity()
sa.myPublicMethod();
这真的取决于你要实现什么样的功能。你可以有每个活动实现与您需要的实例方法的接口,然后让tabhost包含对接口类型的当前活动的参考。如果你这样做,确保每个切换到另一个活动时更新基准。
I need to have a TabHost
consisting of two tabs
where each tab
represented by a separate Activity
. Each Activity
has its own data fields and methods to operate on them. I want each tab
to have its own menu
and for this menu
I need onOptionsItemSelected()
method to be a part of the class
to make it able to invoke methods of this class
.
What the best approach would it be to implement this?
I've started with menu
definition in the class which represents TabHost
, but then faced with a problem I don't have an access to methods of my Activities
. I decided to make methods of Activities
static
to have access to them but finally I would need to define almost all fields of these Activities
as static which doesn't seem to be a good solution for me.
Update:
It became much easier then I thought. In onOptionsItemSelected() you can get the current Activity
of your opened tab
.
SecondActivity sa = (SecondActivity)this.getCurrentActivity()
sa.myPublicMethod();
This really depends on what sort of functionality you are implementing. You could have each activity implement an interface with the instance methods you need and then have the tabhost contain a reference to the current activity of type interface. If you do this, make sure to update the reference each time you switch to another activity.
这篇关于TabHost:如果我需要处理onOptionsItemSelected每个单独的标签/活动是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!