2013-01-12 81 views
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); 
    } 
} 

回答

0

已经有很好的演示你正在寻找什么。

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

还检查了this tutorialthis lecture

+0

感谢您的反馈。我现在已经查看了你的链接,并且据我所知,我没有做任何与他们完全不同的事情。 –

+0

所以它应该工作。也许试试每种设备的单独测试? –

+0

我想通了。当我用getSupportFragmentManager()更改getFragmentManager()的出现时,它按预期工作。啊。 –

相关问题