I'm trying to implement a tab bar with fragments and RadioGroup
i switch fragments like on checked Change of radiogroup like this (saw something like this in sdk examples)
publi
void onCheckedChanged(RadioGroup radioGroup, int id) {
TabInfo newTab = mContent.get(id);
if (newTab != lastTab) {
FragmentTransaction transaction = mActivity.getSupportFragmentManager().beginTransaction();
if (lastTab != null && lastTab.fragment != null) {
transaction.detach(lastTab.fragment);
}
if (newTab.fragment == null) {
newTab.fragment = Fragment.instantiate(mActivity, newTab.getTag());
transaction.add(mContainerId, newTab.fragment);
} else {
transaction.attach(newTab.fragment);
}
lastTab = newTab;
transaction.setCustomAnimations(R.anim.tab_transaction, R.anim.tab_transaction);
transaction.commit();
}
}
but every time this happen attached fragment is created from scratch i.e. called onCreate and so on..
is there any way to preserve fragments from creating over and over again within an activity? also i don't want the back button could switch fragments back;