Reputation: 493
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.
Upvotes: 2
Views: 2298
Reputation: 35
I experimented about tabs in fragment activity. So, may be it is worth to be seen
FragmentTabHost with own fragment stack for each tab
Upvotes: 0
Reputation: 493
I've figured it out after starting a dummy project and doing everything from scratch:
The activity I was using to handle the tabs needs to be a TabActivity and implimented as if I was creating normal Tabs using Activities, except I'll be using separate FragmentActivity "stacks" to manage the content in each tab. Also, the layout I was using for the Tab activity was wrong for what I was doing, I am now using:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
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"
android:padding="5dp">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp" />
</LinearLayout>
</TabHost>
Those were the two major changes, I've uploaded the source for the test project in case anyone wants to reference it for their own projects:
Seperate Fragment Stacks in Tabs
EDIT
Forgot to mention that TabActivity is deprecated so one shouldn't rely on this method for future android versions. I have not figured out how to implement this using a FragmentActivity. That said, it works for my current needs and gives me time to figure out how to do it the new way.
Upvotes: 2