我想获得一个示例代码来显示一个在我的主 FragmentActivity 中使用 TabHost 的新 FragmentActivity。FragmentActivity 由五个选项卡组成,其中一个选项卡必须显示具有选项卡的新活动。我的代码:
TabHost.OnTabChangeListener tabChangeListener = new TabHost.OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
fm = getSupportFragmentManager();
venueFragment = (FragmentVenue) fm.findFragmentByTag("venue");
favoriteFragment = (FragmentFavorite) fm.findFragmentByTag("favorite");
venueTrendingFragment = (FragmentVenueTrending) fm.findFragmentByTag("venuetrendingnow");
searchFragment = (FragmentSearch) fm.findFragmentByTag("search");
accountFragment = (FragmentAccount) fm.findFragmentByTag("account");
android.support.v4.app.FragmentTransaction ft = fm.beginTransaction();
/** Detaches the venueFragment if exists */
if(venueFragment!=null)
ft.detach(venueFragment);
/** Detaches the favoriteFragment if exists */
if(favoriteFragment!=null)
ft.detach(favoriteFragment);
/** Detaches the venueTrendingFragment if exists */
if(venueTrendingFragment!=null)
ft.detach(venueTrendingFragment);
/** Detaches the searchFragment if exists */
if(searchFragment!=null)
ft.detach(searchFragment);
if(accountFragment!=null)
ft.detach(accountFragment);
/** If current tab is venue */
if(tabId.equalsIgnoreCase("venue")){
if(venueFragment==null){
/** Create venueFragment and adding to fragment transaction */
ft.add(R.id.realtabcontent,new FragmentVenue(), "venue");
}else{
/** Bring to the front, if already exists in the fragment transaction */
ft.attach(venueFragment);
}
}else if(tabId.equalsIgnoreCase("favorite")){ /** If current tab is favorite */
if(favoriteFragment==null){
/** Create favoriteFragment and adding to fragment transaction */
ft.add(R.id.realtabcontent,new FragmentFavorite(), "favorite");
}else{
/** Bring to the front, if already exists in the fragment transaction */
ft.attach(favoriteFragment);
}
}else if(tabId.equalsIgnoreCase("venuetrendingnow")){ /** If current tab is venueTrendingFragment */
if(venueTrendingFragment==null){
/** Create venueTrendingFragment and adding to fragment transaction */
ft.add(R.id.realtabcontent,new FragmentVenueTrending(), "venuetrendingnow");
}else{
/** Bring to the front, if already exists in the fragment transaction */
ft.attach(venueTrendingFragment);
}
}else if(tabId.equalsIgnoreCase("search")){ /** If current tab is searchFragment */
if(searchFragment==null){
/** Create searchFragment and adding to fragment transaction */
//ft.add(R.id.realtabcontent,new FragmentSearch(MainActivity.this), "search");
//Intent intent = new Intent(this,SecondTabhost.class);
Intent intent = new Intent(MainActivity.this, SecondTabhost.class);
startActivity(intent);
}else{
/** Bring to the front, if already exists in the fragment transaction */
//ft.attach(searchFragment);
Intent intent = new Intent(MainActivity.this, SecondTabhost.class);
startActivity(intent);
}
}else if(tabId.equalsIgnoreCase("account")){ /** If current tab is accountFragment */
if(accountFragment==null){
/** Create accountFragment and adding to fragment transaction */
ft.add(R.id.realtabcontent,new FragmentAccount(), "account");
}else{
/** Bring to the front, if already exists in the fragment transaction */
ft.attach(accountFragment);
}
}
ft.commit();
}
};
在上面代码中的 else if(tabId.equalsIgnoreCase("search")) 内部,我使用 Intent 转到下一个活动,因此延迟显示下一个具有 tabhost 的 FragmentActivity。我想毫不拖延地显示活动。