2つのフラグメントで構成されるレイアウトのアプリケーションがあります。左側に1つのメニューフラグメント、右側にコンテンツフラグメント。ユーザーがメニューフラグメントのメニュー項目を押すと、コンテンツフラグメントが変更されます。
小さなデバイス(電話)では、最初はメニューフラグメントのみを表示します。ユーザーがメニュー項目を押すと、メニューフラグメント全体がコンテンツフラグメントに置き換えられます。ただし、ユーザーが戻るボタンを押してメニューに戻りたい場合は、アプリケーションが閉じます(バックスタックに何もないかのように)。
以下は私のコードのトリミングされたバージョンです、私は何が間違っていますか?
public class MainActivity extends FragmentActivity implements IMenuListener {
private static final String TAG = MainActivity.class.getName();
private Fragment menuFragment = null;
private Fragment contentFragment = null;
private FragmentManager fm;
private ActionBar actionBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(fm == null) fm = getFragmentManager();
if(actionBar == null) actionBar = getActionBar();
menuFragment = (MenuFragment) fm.findFragmentById(R.id.menuFragment);
contentFragment = (Fragment) fm.findFragmentById(R.id.contentFragment);
if(menuFragment == null) {
menuFragment = new MenuFragment();
replaceFragment(R.id.menuFragment, menuFragment);
}
// If the content fragment has not been initiated, initiate it, as the view exists in XML.
if(contentFragment == null && findViewById(R.id.contentFragment) != null) {
actionBar.setSubtitle(getResources().getString(R.string.actionbar_subtitle_home));
contentFragment = new HomeFragment();
replaceFragment(R.id.contentFragment, contentFragment);
}
// If the content fragment has not been initiated and the view does not exist in XML, don't initiate it.
else if(contentFragment == null && findViewById(R.id.contentFragment) == null) {
// Basically do nothing.
actionBar.setSubtitle(getResources().getString(R.string.actionbar_subtitle_menu));
}
}
// Menu item clicked, change fragment
@Override
public void onMenuItemSelected(MenuItem item) {
if(contentFragment != null && contentFragment.isVisible()) {
// Custom MenuItem class has a Fragment field
replaceFragment(R.id.contentFragment, item.getFragment());
contentFragment = item.getFragment();
} else {
if(deviceIsPhoneInPortaitOrientation()) {
replaceFragment(R.id.menuFragment, item.getFragment());
}
}
}
/**
* Replace current fragment
* @param container The resource id of the container view
* @param newFragment The new fragment which should be placed in the given container
*/
private void replaceFragment(int container, Fragment newFragment) {
FragmentTransaction ft = fm.beginTransaction();
ft.addToBackStack(null); // TODO Not working as expected
ft.replace(container, newFragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.commit();
}
/**
* Determines whether the current device is a phone with current orientation
* set to portrait or not.
* @return true if it is, false otherwise
*/
public boolean deviceIsPhoneInPortaitOrientation() {
return (!getResources().getBoolean(R.bool.isTablet) &&
getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT);
}
}