Removing a tab and the activity (intent) inside of it from a TabHost

扶醉桌前 提交于 2019-12-01 19:24:19
wired00

Try changing current tab to 0.

Something like:

getTabHost().setCurrentTab(0);
getTabHost().clearAllTabs();

I was reading that calling clearAllTabs(); will throw a nullpointerexception if you don't set the tabhost to the first tab (.setCurrentTab(0)) before calling (.clearAllTabs())

Also this answer may help? (How to remove tab from TabHost)

I would suggest a different approach. You can use an ActivityGroup to build your own TabControl. As you are using normal Buttons (or similar controls just as you like) you can easyly arrange/create/remove them as needed.

I can't dump the whole code here but that is basically what I did when I had the same problem:

  • Create an Activity inherited from ActivityGroup
  • Place a ViewGroup in your layout where you want to show the sub-activities
  • Setup your buttons as needed (LinearLayout works fine with a variable count of buttons)
  • Start activites thru getLocalActivityManager().startActivity() as needed

You can now add/remove buttons as you like. The Activites follow the Android lifecycle so you don't have to delete them yourself.

You might have to implement onBackPressed on your ActivityGroup to properly handle the history but that depends on the project.

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