我正在按照Google文档设计的方式构建一个导航抽屉,但我遇到了一个问题,即Fragment没有被替换。应用首次加载时会加载默认的Fragment。单击抽屉列表上的其他项会留下空视图。但是在旋转设备后,将加载所选的Fragment。http://developer.android.com/training/implementing-navigation/nav-drawer.html
public void selectNavActivty(int position){
// TODO Changing between the different screens selection
fragment = null;
switch (position) {
case 0:
fragment = OverLay.newInstance();
break;
case 1:
fragment = Dummy.newInstance();
break;
}
if(fragment != null) {
// attach added to handle viewpager fragments
FragmentTransaction trans = getSupportFragmentManager().beginTransaction();
trans.replace(R.id.content_frame, fragment).attach(fragment)
.addToBackStack(null);
trans.commit();
getFragmentManager().executePendingTransactions();
} else {
Log.d("Drawer Activity","Error in creating Fragment");
}
}
executePendingTransactions
并在现有片段之上添加片段造成的,这通常是不好的做法,因为两者都会保留在内存中并保持活动状态。 - Konstantin Loginov