摘要:Android - 替換目前Fragment ,並Back回上一個Fragment
替換目前Fragment
FragmentManager fragmentManger = getFragmentManager();
final FragmentTransaction fragmentTransaction = fragmentManger.beginTransaction();
fragmentTransaction.replace(R.id.content_frame, fragment);
fragmentTransaction.addToBackStack(fragment.getClass().getName());
fragmentTransaction.commit();
按Back鍵時,回上個Fragment,實作在Activity
@Override
public void onBackPressed() {
int count = getFragmentManager().getBackStackEntryCount();
if (count == 0) {
super.onBackPressed();
} else {
getFragmentManager().popBackStack();
}
}