Android 获取当前片段的ID

Android get ID of current fragment

我实现了导航抽屉,但问题是当按下后退按钮时,绘制中选中的项目与当前片段不对应(仅通过导航抽屉实际添加的最后一个片段)

这里有一个解决方案:Updating the Navigation Drawer (with DrawerLayout) when back button is pressed

解决方案如下:

getSupportFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
            @Override
            public void onBackStackChanged() {
                FragmentManager fm = getSupportFragmentManager();
                String stackName = null;
                for(int entry = 0; entry < fm.getBackStackEntryCount(); entry++){
                    stackName = fm.getBackStackEntryAt(entry).getName();
                    Log.i("BC", "stackEntry" + entry);
                }
                if (stackName == "Fragment1"){
                    mDrawerList.setItemChecked(0, true);
                } else if (stackName == "Fragment2") {
                    mDrawerList.setItemChecked(1, true);
                }

            });

问题是有条件的。当您有很多项目时,必须手动更改要检查的导航项目的索引是一件很痛苦的事情

我想做的是监听 backstack 中的变化,如果检测到,获取当前片段的 id(在布局文件中定义)并将其传递给 navigationView.setCheckedItem() 以自动检查相应的导航项。像这样:

navigationView = (NavigationView) findViewById(R.id.nav_view);

navigationView.setCheckedItem(R.id.nav_new);

在我的主要活动 class 中,如何获取 R.id.xxx 形式的当前片段的 ID,以便我可以将其传递给 setCheckedItem

你可以这样做

在添加片段时执行此操作

FragmentTransaction.addToBackStack(""+R.id.nav_new); // Use the id of navigation item which was clicked.

然后

getSupportFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
    @Override
    public void onBackStackChanged() {
        FragmentManager fm = getSupportFragmentManager();
        String stackName = null;
        for(int entry = 0; entry < fm.getBackStackEntryCount(); entry++){
            stackName = fm.getBackStackEntryAt(entry).getName();
            Log.i("BC", "stackEntry" + entry);
        }

        int navItemId = -1;
        try{
            navItemId = Integer.parseInt(stackName);    
        } catch(NumberFormatException nfe) {
        }

        if(navItemId > 0){
            navigationView = (NavigationView) findViewById(R.id.nav_view);
            navigationView.setCheckedItem(navItemId);
        }
        else
            Log.e("", "Couldn't find the id");
    });

注意: 当您在 FragmentManager.BackStackEntry[=] 上使用 getName() 时,您将传入 addToBackStack 的任何内容都会返回给您15=]

you can handle it manually.
Make a function in base class to highlight desired item like
mDrawerList.setItemChecked(INDEX NUMBER, true);

Trick is make a model class for side menu items in which INDEX NUMBER is saved.
When you replace new fragment call that function and store currentFragment a well.
and when you press back button ovverides

 public boolean onKeyDown(int keyCode, KeyEvent event) {}
and call below function

public void popAFragment(){
 FragmentManager fragmentManager = getSupportFragmentManager();
        if(fragmentManager != null){
            int c = fragmentManager.getBackStackEntryCount();
            if(c>0){
                currentFragment = (BaseFragment) fragmentManager.getFragments().get(c-1);
                fragmentManager.popBackStack();
                setPosition(currentFragment.INDEX_NUMBER);
            }
            else {
finish();
}
}