回到之前的片段无法正常工作

时间:2014-12-10 10:45:51

标签: android android-fragments back

我正在使用导航抽屉,当我选择一个项目时,它会正确地将前一个片段替换为我选择的片段,但如果我打开另一个片段,然后单击后退按钮,它将转到上一个活动而不是以前的片段,这是做错了什么?

private void selectItem(int position) {
    Fragment newFragment;
    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    switch (position){
        case 0:
            //Update
            newFragment = new UpdateFragment();
            transaction.replace(R.id.content_frame, newFragment);
            transaction.addToBackStack(null);
            transaction.commit();
            listView.setItemChecked(position, true);
            setTitle(navigationDrawerItems[position]);
            break;
        case 1:
            //Maps
            newFragment = new TimetableFragment();
            transaction.replace(R.id.content_frame, newFragment);
            transaction.addToBackStack(null);
            transaction.commit();
            listView.setItemChecked(position, true);
            setTitle(navigationDrawerItems[position]);
            break;
        case 2:
            //Timetables
            newFragment = new TimetableFragment();
            transaction.replace(R.id.content_frame, newFragment);
            transaction.addToBackStack(null);
            transaction.commit();
            listView.setItemChecked(position, true);
            setTitle(navigationDrawerItems[position]);
            break;
        case 3:
            //Notes
            newFragment = new TimetableFragment();
            transaction.replace(R.id.content_frame, newFragment);
            transaction.addToBackStack(null);
            transaction.commit();
            listView.setItemChecked(position, true);
            setTitle(navigationDrawerItems[position]);
            break;
        case 4:
            //About
            newFragment = new AboutFragment();
            transaction.replace(R.id.content_frame, newFragment);
            transaction.addToBackStack(null);
            transaction.commit();
            listView.setItemChecked(position, true);
            setTitle(navigationDrawerItems[position]);
            break;
        case 5:
            //Settings
            newFragment = new SettingsFragment();
            transaction.replace(R.id.content_frame, newFragment);
            transaction.addToBackStack(null);
            transaction.commit();
            listView.setItemChecked(position, true);
            setTitle(navigationDrawerItems[position]);
            break;
    }
    //listView.setItemChecked(position, true);
    //setTitle(navigationDrawerItems[position]);
    drawerLayout.closeDrawer(listView);
}

2 个答案:

答案 0 :(得分:3)

您可以在getFragmentManager().popBackStack()中使用onBackPressed方法:

@Override
public void onBackPressed() {
    if (getFragmentManager().getBackStackEntryCount() > 0) {
         getFragmentManager().popBackStack();
         getFragmentManager().beginTransaction().commit();
    }
    else {
        super.onBackPressed();
    }
}

不要忘记在BackStack中添加片段,如:

transaction.addToBackStack(null);

编辑:

要在操作栏中显示当前片段名称,您可以在FragmentActivity的onResume上获取它:

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();

    FragmentManager fragManager = this.getSupportFragmentManager();
    int count = this.getSupportFragmentManager().getBackStackEntryCount();

    // Fetch last fragment
    Fragment fragment = fragManager.getFragments().get(count>0?count-1:count);

    // Check with your possible Fragment class
    if( fragment instanceof OneFragment)
    {
        setTitle("OneFragment");
    }
    else if(fragment instanceof TwoFragment)
    {
        setTitle("TwoFragment");
    }
}

希望它有助于ツ

答案 1 :(得分:-1)

您正在向Backstack添加null -

transaction.addToBackStack(null);

将此null替换为片段的标记名称。

它会起作用。