I have 3 fragments and an activity. I want to enable tabs on the ActionBar
and assign a Fragment
to each of the 3 tabs. How do I hook that up correctly?
ORIGINAL POST
I have an app that I'm developing using the Google I/O app as a guide. I've implemented tabs into the ActionBar
. They seem to be working until the tablet's orientation changes. For example, all 3 tabs have a Fragment
. I can switch between them just fine, but when I change the orientation, whatever the Fragment
I was last viewing stays visible, but clicking the tabs no longer changes the view... like they have become disconnected. As expected, going back to the original orientation does not "fix" it.
I've looked into saving and restoring state, but I'm not seeing how those would help.
EDIT
module level:
Fragment mFragmentA = (Fragment) new AFragmentTab();
Fragment mFragmentB = (Fragment) new BFragmentTab();
Fragment mFragmentC = (Fragment) new CFragmentTab();
I have something like this in the activity's onCreate
:
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayShowTitleEnabled(false);
ActionBar.Tab tabA = actionBar.newTab().setText("text a");
ActionBar.Tab tabB = actionBar.newTab().setText("text b");
ActionBar.Tab tabC = actionBar.newTab().setText("text c");
tabA.setTabListener(this);
tabB.setTabListener(this);
tabC.setTabListener(this);
actionBar.addTab(tabA);
actionBar.addTab(tabB);
actionBar.addTab(tabC);
and a TabListener
like this:
EDIT this is removed
class MyTabListener implements ActionBar.TabListener {
private Fragment mFragment;
// Called to create an instance of the listener when adding a new tab
public MyTabListener(Fragment fragment) {
mFragment = fragment;
}
public void onTabSelected(Tab tab, FragmentTransaction ft) {
ft.add(R.id.fragment_content, mFragment, null);
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
ft.remove(mFragment);
}
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// do nothing
}
}
EDIT
I've moved the TabListener
. Instead of it being a separate class, I implement the TabListener
on the Activity
. Then on the Selected
and Unselected
methods I have something like:
public void onTabSelected(Tab tab, FragmentTransaction ft) {
switch (tab.getPosition()) {
case 0:
ft.add(R.id.fragment_content, mFragmentA, null);
break;
case 1:
ft.add(R.id.fragment_content, mFragmentB, null);
break;
case 2:
ft.add(R.id.fragment_content, mFragmentC, null);
break;
}
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
switch (tab.getPosition()) {
case 0:
ft.remove(mFragmentA);
break;
case 1:
ft.remove(mFragmentB);
break;
case 2:
ft.remove(mFragmentC);
break;
}
}
It's still doing the same thing. I really don't know what's happening.
It looks like my answer can be found here: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/FragmentTabs.html
This worked great for me! I had this problem where the activities were overlapping everytime I changed the tabs:
Thanks again!
The link provided by @Metallicraft is dead so here is some help for those who still stumbles upon this post. Go here for help with implementing tabs.
To see/read the original sample you can go to following folder if you've installed android development environment:
android-sdk\samples\android-14\ApiDemos\src\com\example\android\apis\app\FragmentTabs.java
Also if you have problem with fragments overlapping after orientation change its properly related to savedinstancestate passed on. Here is a copy paste from a google tutorial that explains the fragment overlapping issue and how to avoid it:
I had more or less the same problem, but the solutions presented above did not seem to work out in my situation. Eventually I found the following solution:
Create a new Android Sample project, choose the Support4Demos sample. You will find the FragmentTabs sample there.