Is there a way to display fragments in TabHost?

安稳与你 提交于 2020-01-12 05:56:25

问题


I'd like to show a different fragment in a TabHost, for every tab. But it seems that there's no easy way to do this. TabHost can only accept A. Views or B. Intents that launch Activities when the user selects a tab.

Going with A means that I have to initialize every fragment and load them into container Views that are given to the TabHost. But I want these fragments to load only when needed - when the user selects their tab that is.

Going with B means that I load the fragments into separate Activities for each tab. But I'd like the fragment to be able to reach the "original" parent Activity, not just some shell Activity that hosts them in a tab content.

Am I missing something? Is there a way to manage fragments with TabHost properly?


回答1:


Some hits here:

  1. fragment Support with Tabhost
  2. Android 1.6 & Fragment & Tabhost

For what I understood, "link" a fragment inside a TabHost isn't allowed, you need to create activities that include your fragments and then call these activities from TabHost.

EDIT the "official solution" from Google, and with nice animations.



来源:https://stackoverflow.com/questions/6827869/is-there-a-way-to-display-fragments-in-tabhost

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