无法让片段重叠工作,我做错了什么?

时间:2013-01-12 18:18:03

标签: android android-fragments back-stack

我有一个包含两个片段的布局的应用程序。左侧是一个菜单片段,右侧是内容片段。当用户按下菜单片段中的菜单项时,内容片段会发生变化。

在小型设备(手机)上我最初只显示菜单片段。当用户按下菜单项时,整个菜单片段被内容片段替换。但是,当用户想要通过按后退按钮返回菜单时,应用程序将关闭(就像后面的堆栈中没有任何内容一样)。

以下是我的代码的修剪版本,我做错了什么?

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

1 个答案:

答案 0 :(得分:0)

已经很好地证明了你的目标。

在“布局”演示中查看actionBarSherlock fragments sample(或谷歌提供的那些,它们大致相同)。

还可以查看this tutorialthis lecture