I am creating an app that has three tabs on the main screen. Each of the tabs I have the content set as separate Activities. The reason I used activities instead of Fragments is because inside one of the tabs I need to be able to have Fragments and nested Fragments are not supported.
At first, I was extending TabActivity to do so, but then I realized that it is deprecated so then I started looking around. I found someone suggested using a ActivityGroup here, but… that too is deprecated.
It seems all the solutions on StackExchange to the problem I am having, contain deprecated functionality. Such as, Android: TabHost without TabActivity , and Android – Tabhost working in Activity class
What is the correct way to have a TabHost and allow the tabs to contain Fragments without using deprecated functionality?
Below is what I currently have
import android.app.ActivityGroup;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
public class HomeTabs extends ActivityGroup {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost host = (TabHost)findViewById(android.R.id.tabhost);
LocalActivityManager mlam = new LocalActivityManager(this, false);
mlam.dispatchCreate(savedInstanceState);
host.setup(mlam);
TabSpec spec = host.newTabSpec("Home");
spec.setContent(new Intent(this, HomeActivity.class));
spec.setIndicator("Home");
host.addTab(spec);
spec = host.newTabSpec("Browse");
spec.setContent(new Intent(this, BrowseActivity.class));
spec.setIndicator("Browse");
host.addTab(spec);
spec = host.newTabSpec("About");
spec.setContent(new Intent(this, AboutActivity.class));
spec.setIndicator("About");
host.addTab(spec);
}
}
Put views in the tabs, whether statically in the layout as immediate children of the
FrameLayoutinside theTabHost, or dynamically via the variant ofsetContent()that takes aTabContentFactory. Those views can, AFAIK, take things that manage fragments (e.g.,ViewPager), or possibly fragments directly themselves via<fragment>elements.Or, don’t use
TabHost. Use tabs in the action bar, using ActionBarSherlock for backwards compatibility. Or, use aViewPager. Or, use aViewFlipperand aTabWidget(which, despite the docs, does not appear to have an actual dependency onTabHost). Or, use aViewFlipperand some other control mechanism to switch between “tabs”.Or, get rid of your “fragment within the tab content”.