4

这就是我使用导航抽屉显示片段的方式:

private void displayView(int position) {
            // update the main content by replacing fragments
            Fragment fragment = null;
            switch (position) {
            case 0:
                fragment = new Fragment0();
                break;
            case 1:
                fragment = new Fragment1();
                break;
            case 2:
                fragment = new Fragment2();
                break;
            case 3:
                fragment = new Fragment3();
                break;
            case 4:
                fragment = new Fragment4();
                break;
            case 5:
                fragment = new Fragment5();

                }

            default:
                break;
            }

            if (fragment != null) {
                FragmentManager fragmentManager = getFragmentManager();
                fragmentManager.beginTransaction()
                        .replace(R.id.frame_container, fragment)
                        .addToBackStack(null)
                        .commit();

                // update selected item and title, then close the drawer
                mDrawerList.setItemChecked(position, true);
                mDrawerList.setSelection(position);
                setTitle(navMenuTitles[position]);
                mDrawerLayout.closeDrawer(drawerll);
            } else {
                // error in creating fragment
                Log.e("MainActivity", "Error in creating fragment");
            }
        }

假设如果用户导航到Fragment 2-> Fragment 5-> Fragment3.

如果用户单击back button我需要descending order在每个片段上显示 和name,当他转到该特定片段时,我将显示片段,fragment即:

Fragment 3-> Fragment 5-> Fragment2.

这是我尝试过的:

第一种方法:

FragmentManager fm = getSupportFragmentManager();
int count = fm.getBackStackEntryCount();
for(int i = 0; i < count; ++i) {
    fm.popBackStackImmediate();
}

第二种方法:

fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);

我无法让这个工作**(即..我无法将它移回我来自的片段**)并且无法根据后台堆栈显示片段的名称

任何帮助是极大的赞赏。

4

4 回答 4

1

1. Handle back

You can use getSupportFragmentManager().popBackStack() method in onBackPressed:

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

Don't forget to add the fragment in BackStack like :

transaction.addToBackStack(null);

2. Display Name

To show the current fragment name in actionbar, you can get it on onResume of your Fragment :

@Override
protected void onResume() {
    super.onResume();

    ((FrgmentActivtiyName)getActivity).changeTitle("title");
}

Define a method which set the title in the actionbar in your FrgmentActivity:

public void changeTitle(String titleToSet) {
   // set title as titleToSet
}

Hope it helps ツ</p>

于 2014-12-11T08:58:00.363 回答
1

我猜您的导航工作正常,您面临的问题是您可以使用的标题

public class SampleFragment extends Fragment{

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

                // Creating view correspoding to the fragment
        View v = inflater.inflate(R.layout.fragment_layout, container, false);

        // Updating the action bar title
        getActivity().getActionBar().setTitle("Screen name");


        return v;
    }
}

在每个片段中。并且每当您按下 fragmemt 的 onCreateView 方法(从后台堆栈中弹出的方法)时,都会调用该标题,并且该标题将再次设置为替换旧标题。

于 2014-12-11T09:09:41.007 回答
0

请试试这个:

在包含片段的活动中,通过以下方式覆盖 onbackPressed() 方法,

@Override
public void onBackPressed() {
    if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
         getSupportFragmentManager().popBackStack();
         getSupportFragmentManager().beginTransaction().commit();
    }
    else {
        super.onBackPressed();
    }
}
于 2014-12-11T07:44:06.850 回答
-1
if (fragment != null) {

        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.frame_container, fragment).commit();
        menuList.setItemChecked(position, true);
        menuList.setSelection(position);
    }
于 2014-12-11T07:40:34.130 回答