I am attempting to add Tabs to an existing app to add more functionality I've been able to implement tabs and also move everything to Fragments. However, the way I have it setup at the moment doesn't preserve the stack per tab. So basically I have a main FrameActivity that handles the tabs and attaches the fragments to each tab.
During my research I found this thread: https://stackoverflow.com/a/7480080/792407
The example he gives makes a lot of sense but I can't seem to get the fragments to display. So let me explain what I'm doing to make sure I understand it correctly:
I have a main tab activity which extends FragmentActivity and handles the tabs. Layout looks like:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabHost
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TabWidget
android:id="@android:id/tabs"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
/>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0"/>
<FrameLayout
android:id="@+android:id/realtabcontent"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
</TabHost>
</LinearLayout>
Within this activity I initialize my tabs:
mTabHost = getTabHost();
Resources res = getResources();
Intent intent;
TabHost.TabSpec spec;
//search tab
intent = new Intent().setClass(this, searchFragmentStack.class);
spec = mTabHost.newTabSpec("search").setIndicator("Search",res.getDrawable(R.drawable.ic_tab_search)).setContent(intent);
mTabHost.addTab(spec);
//home tab
intent = new Intent().setClass(this, homeFragmentStack.class);
spec = mTabHost.newTabSpec("home").setIndicator("Home",res.getDrawable(R.drawable.ic_tab_home)).setContent(intent);
mTabHost.addTab(spec);
The stack classes I'm using look like:
public class searchFragmentStack extends ActivityInTab {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
navigateTo(new search());
}
}
The ActivityInTab abstract class is the same code he used in the thread:
abstract class ActivityInTab extends FragmentActivity { // FragmentActivity is just Activity for the support library.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabs_layout);
}
/**
* Navigates to a new fragment, which is added in the fragment container
* view.
*
* @param newFragment
*/
protected void navigateTo(Fragment newFragment) {
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
ft.add(R.id.content, newFragment);
ft.addToBackStack(null);
ft.commit();
}
@Override
public void onBackPressed() {
FragmentManager manager = getSupportFragmentManager();
if (manager.getBackStackEntryCount() > 0) {
// If there are back-stack entries, leave the FragmentActivity
// implementation take care of them.
super.onBackPressed();
} else {
// Otherwise, ask user if he wants to leave :)
//showExitDialog();
}
}
}
and the layout for the stack is again based on his example:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/content"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:isScrollContainer="true">
</RelativeLayout>
And that's pretty much it. All I get are black screens in the tabs which makes me think it's a layout issue or I'm just doing it wrong. Does this make sense? Is there a better way? Am I missing something? Any help will be greatly appreciated.