在BottomNavigationView中按下后退按钮时,按钮保持不变

时间:2018-12-17 10:27:28

标签: android bottomnavigationview

我在底部导航栏上的dashbordActivity中为用户提供了三个片段。在后退按钮上,按正确的上一个片段视图,但底部导航按钮未更改。

在我从底部导航中选择配置文件并按返回按钮后,已经选择了第一个图像主页片段中的

。片段已更改,但底部导航项未更改。   enter image description here

这是我的底部导航视图.....

  private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
        = new BottomNavigationView.OnNavigationItemSelectedListener() {
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {

        switch (item.getItemId()) {
            case R.id.navigation_home:
                setTitle("Home");
                fragment = new FirstFragment();
                loadFragment(fragment);
                return true;
            case R.id.navigation_favorite:

                setTitle("favourite");
                fragment = new MyFavoriteAdsFragment();
                loadFragment(fragment);
                return true;
            case R.id.navigation_profile:

                setTitle("Profile");
                fragment = new ProfileFragment();
                loadFragment(fragment);
                return true;
            case R.id.navigation_myads:
                setTitle("Chat");
                fragment = new MyAdsFragment();
                loadFragment(fragment);
                return true;
            case R.id.navigation_chat:
                //   mTextMessage.setText(R.string.title_chat);
                setTitle("Chat");
                fragment = new TabFragmentChatUserList();
                loadFragment(fragment);
              /* Intent i=new Intent(getApplicationContext(),Chat.class);
               startActivity(i);*/
                return true;
        }
        return false;
    }
};


  private void loadFragment(Fragment fragment) {
    // load fragment
     transaction = getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.frame_container, fragment);
    transaction.addToBackStack("TAG");
    transaction.commit();
  /*  fragmentManager.beginTransaction().replace(R.id.home_container, frag).addToBackStack(null).commit();*/
}

我尝试过后退...

   @Override
  public void onBackPressed()
  {

 // if your using fragment then you can do this way
        int fragments 
  =getSupportFragmentManager().getBackStackEntryCount();
        if (fragments == 1) {
            new AlertDialog.Builder(this)
                    .setMessage("Are you sure you want to exit?")
                    .setCancelable(false)
                    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            finish();
                        }
                    })
                    .setNegativeButton("No", null)
                    .show();


        } else {
            if (getFragmentManager().getBackStackEntryCount() > 1) {
                getFragmentManager().popBackStack();
                if(navigation.getSelectedItemId () != R.id.navigation_favorite)
                {
                    navigation.setSelectedItemId(R.id.navigation_favorite);
                }
                else if(navigation.getSelectedItemId () != R.id.navigation_chat)
                {
                    navigation.setSelectedItemId(R.id.navigation_chat);
                }
                else if(navigation.getSelectedItemId () != R.id.navigation_profile)
                {
                    navigation.setSelectedItemId(R.id.navigation_profile);
                }
                else
                {
                    super.onBackPressed();
                }

            } else {
                super.onBackPressed();
            }
        }

2 个答案:

答案 0 :(得分:2)

您需要检查堆栈,并将侦听器添加到片段管理器中,然后根据片段,您需要根据片段设置特定的按钮检查。

fragmentManager.addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
        @Override
        public void onBackStackChanged() {
            // If the stack decreases it means I clicked the back button
            if( fragmentManager.getBackStackEntryCount() <= count){
                //check your position based on selected fragment and set it accordingly.
                navigation.getMenu().getItem(your_pos).setChecked(true);
        }
    }
});

答案 1 :(得分:0)

您可以通过以下方式自定义onBackPressed

 @Override
    public void onBackPressed() {
    // if your using fragment then you can do this way
    int fragments
            = getSupportFragmentManager().getBackStackEntryCount();
    if (fragments == 1) {
        new AlertDialog.Builder(this)
                .setMessage("Are you sure you want to exit?")
                .setCancelable(false)
                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        finish();
                    }
                })
                .setNegativeButton("No", null)
                .show();


    } else {
        if (getSupportFragmentManager().getBackStackEntryCount() > 1) {
            getSupportFragmentManager().popBackStack();
            Fragment selectedFragment = null;
            List<Fragment> fragments = getSupportFragmentManager.getFragments();
            for (Fragment fragment : fragments) {
                if (fragment != null && fragment.isVisible()) {
                    selectedFragment = fragment;
                    break;
                }
            }
            if (selectedFragment instanceof yourFirstFragment) {
                navigation.setSelectedItemId(R.id.your_first_item);
            } if (selectedFragment instanceof yourSecondFragment) {
                navigation.setSelectedItemId(R.id.your_second_item);
            } if (selectedFragment instanceof yourThirdFragment) {
                navigation.setSelectedItemId(R.id.your_third_item);
            } else {
                super.onBackPressed();
            }

        } else {
            super.onBackPressed();
        }
    }