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);
    }
}
4

1 回答 1

0

已经很好地演示了您正在寻找的内容。

在“布局”演示中检查actionBarSherlock 片段示例(或谷歌提供的,它们大致相同)。

还可以查看本教程本讲座

于 2013-01-12T21:47:20.723 回答