/* The following are each of the ActionBar.TabListener callbacks */ @Override public void onTabSelected(CompatTab tab, FragmentTransaction ft) { // Check if the fragment is already initialized Fragment fragment = tab.getFragment(); if (fragment == null) { // If not, instantiate and add it to the activity fragment = Fragment.instantiate(mActivity, mClass.getName()); tab.setFragment(fragment); ft.add(android.R.id.tabcontent, fragment, tab.getTag()); } else { // If it exists, simply attach it in order to show it ft.attach(fragment); } }
@Override public void onTabUnselected(CompatTab tab, FragmentTransaction ft) { Fragment fragment = tab.getFragment(); if (fragment != null) { // Detach the fragment, because another one is being attached ft.detach(fragment); } }