如何移除当前片段并显示先前的片段?就像我按“返回”按钮一样。
我正在使用以下结构:
FragmentManager fm=getFragmentManager();
FragmentTransaction ft=fm.beginTransaction();
ft.remove(fragment).commit();
但它只是删除当前片段,而不显示之前的片段。
如何移除当前片段并显示先前的片段?就像我按“返回”按钮一样。
我正在使用以下结构:
FragmentManager fm=getFragmentManager();
FragmentTransaction ft=fm.beginTransaction();
ft.remove(fragment).commit();
FragmentTransaction.addToBackStack(null)
,然后在想要移除它时调用FragmentManager.popBackStack()
。 @Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
if( this.getFragmentManager().getBackStackEntryCount() != 0 ){
this.getFragmentManager().popBackStack();
return true;
}
// If there are no fragments on stack perform the original back button event
}
return super.onKeyDown(keyCode, event);
}
在更改片段的位置执行以下操作:
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(android.R.id.content, new YourFragmentName());
transaction.addToBackStack(null); // this is needed for the above code to work
transaction.commit();
FragmentManager fm=getSupportFragmentManager();
FragmentTransaction ft=fm.beginTransaction();
ft.remove(fragment).commit();
previousFragment=(SherlockFragment)getSupportFragmentManager()
.findFragmentByTag(""+currentTagNum);
getSupportFragmentManager().beginTransaction()
.show(mFragment)
.commit();