Navbar无法使用onbackPressed片段正确更新

时间:2018-12-13 18:27:29

标签: java android android-fragments onbackpressed

我最近在片段上实现了onBackPressed方法,效果很好,但是导航栏有一个小错误...

图片胜于长篇演讲!

在此图片中:该应用已启动

the app is launched

在这张照片中:我单击了右侧的最后一个按钮(信息)

I was on the first button on the left (The map) then i clicked the last button on the right (Infos)

在这张照片中:我想回到第一个功能(地图),所以我单击了回车键

I want to go back on the first fonctionnality (The map) so i clicked on the return key

如您所见,在最后一个屏幕中,显示了很好的片段,但是导航栏的“信息”按钮仍然被单击!

这是我启动片段的代码:

private BottomNavigationView.OnNavigationItemSelectedListener navListener =
        new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                Fragment selectedFragment = null;

                switch (item.getItemId()) {
                    case R.id.nav_profile:
                        AccessToken accessToken = AccessToken.getCurrentAccessToken();
                        if(token ==null) {
                            selectedFragment = new EnregistrementFragment();
                            //selectedFragment = new ProfileFragment();
                        }else{
                            selectedFragment = new ProfileFragment();
                        }
                        break;
                    case R.id.nav_map:
                        selectedFragment = new LocalisationFragment();
                        break;
                    case R.id.nav_group:
                        selectedFragment = new ScoreFragment();
                        break;
                    case R.id.nav_faq:
                        selectedFragment = new InfosFragment();
                        break;
                    case R.id.nav_favorite:
                        selectedFragment = new FavoriteFragment();
                        break;
                }
                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                transaction.replace(R.id.fragment_container, selectedFragment);
                transaction.addToBackStack("FirstFragment");
                transaction.commit();

                return true;
            }
        };

我的方法onBackPressed():

    public void onBackPressed() {
    int count = getFragmentManager().getBackStackEntryCount();

    if (count == 0) {
        super.onBackPressed();
        //additional code
    } else {
        getFragmentManager().popBackStack();
    }
}

我的问题是:您是否有刷新导航栏的想法?

先谢谢您,真的很抱歉英语不好!

0 个答案:

没有答案