Set a Tab from an activity

徘徊边缘 提交于 2019-12-13 04:04:58

问题


I have a TabActivity and inside 5 tabs with 5 activities each one. I want to set one of my tabs but not from the TabActivity(clicking on the tab on the top) but from an activity of any tab, for example clicking on a button of activity 3(which is asociated with the 3rd tab). In my TabActivity there is a variable mTabhost, which I can use to set the selected tab with

mTabHost.setCurrentTab(0);//home

I guess I need access to that variable from Activity1-2-3-4, and doing that variable static doesnt work.


回答1:


Say you have a MyTabActivity as TabActivity, which hosts 5 Activity(ies). If you want to change the Tabs inside SecondActivity, you would write the code something like this.

MyTabActivity myTabs = (MyTabActivity) this.getParent();

Here you have your MyTabActvity you can change the tabs like:

myTabs.getTabHost().setCurrentTab(index);



回答2:


Save this mHost var instance in singleton class say Utility class . Then access particular var from Utility Class in different activity and set it accordingly. It works. I 've used it in my several projects.



来源:https://stackoverflow.com/questions/6532455/set-a-tab-from-an-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!