I'm trying to implement a two tabs and also have a navigation drawer. I'm able to do both separately but when I try to combine the two I run into problems. I originally had the code for creating my two tabs and the nav drawer inside my MainActivity which was a Fragment Activity at the time, but when it became clear that for me to have a Home option on the nav drawer I decided to refactor the code for tabs into a fragment with tabs that wold be then filled with two fragments as before.
On launching appears to be fine but when I click on the home option in the nav drawer the fragment in each tab seems to be stripped away and two more tabs are added each time I click on the option.
private void selectItem(int position) {
Fragment fragment = new NullFragment();
switch (position){
case 0:
fragment = new HomeFragment();
break;
case 1:
//Placeholder
fragment = new PlanetFragment();
}
// update the main content by replacing fragments
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
// update selected item and title, then close the drawer
drawerListView.setItemChecked(position, true);
setTitle(drawerItemTitles[position]);
drawerLayout.closeDrawer(drawerListView);
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}
HomeFragment.java
public class HomeFragment extends Fragment implements ActionBar.TabListener {
AppSectionsPagerAdapter appSectionsPagerAdapter;
ViewPager viewPager;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View rootView = null;
/*if(savedInstanceState == null){*/
rootView = inflater.inflate(R.layout.activity_home, container, false);
appSectionsPagerAdapter = new AppSectionsPagerAdapter(getFragmentManager());
final ActionBar actionBar = getActivity().getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
viewPager = (ViewPager) rootView.findViewById(R.id.pager);
viewPager.setAdapter(appSectionsPagerAdapter);
viewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
actionBar.addTab(
actionBar.newTab()
.setIcon(R.drawable.ic_sync_to_cloud)
.setTabListener(this));
actionBar.addTab(
actionBar.newTab()
.setIcon(R.drawable.ic_send_to_device)
.setTabListener(this));
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
return rootView;
}
@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
// When the given tab is selected, switch to the corresponding page in the ViewPager.
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
}
I assume this is because a new instance of HomeFragment is created each time and the only thing in its layout is a ViewPager. Is there a way to do this so a new instance isn't created but instead the fragment can be stored and restored. Let me know if you need to see more code.