0

I have two fragments in my app namely HomeFragment and FeedbackFragment. In addition to switching fragments on tab buttons, I would also need to switch between fragments on a button click that is within the fragment. The tab items are highlighted properly on switching fragments using tab clicks. But the tab items does not get highlighted when i switch to other fragment on a button click from fragment1

Below is the code used to switch across fragments on button click and it works.

    FragmentTransaction fragmentTransaction =  getActivity().getSupportFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.fragment, fragment);
    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();

But the respective tab icon of FeedbackFragment is not highlighted. Currently the navigation item of HomeFragment remains highlighted even after FeedbackFragment is replaced. How do I highlight the menu item of Feedback Fragment ?

I tried below approaches but nothing worked:

BottomNavigationView bottomNavigationView = (BottomNavigationView)   findViewById(R.id.navigation);

View view = bottomNavigationView.findViewById(R.id.tab_calls);
view.performClick();

Also,

  MainActivity.mBottomBar.selectTabAtPosition(2);

Nothing worked. Please help.

2 Answers2

0

Try using the method to select a tab as if it was tapped:

bottomNavigationView.setSelectedItemId(R.id.tab_calls)
Xenolion
  • 12,035
  • 7
  • 33
  • 48
  • Yes, I did try all of this, but of no use :( The given examples are to highlight the tab item icon on click of tab items. But In my case I want to highlight the bottomNavigationView item on button click from another fragment. The highlighting of item works perfectly fine, when i click on the tab item to switch across fragments. But I would need to switch on fragments from a local button within my fragment. In such cases am not able to access bottomNavigationView that is available in MainActivity from my fragment. – AndroidNewBee Jun 18 '19 at 03:00
  • Have you found the solution yet? – Xenolion Jun 18 '19 at 17:42
0

What I have done now is in onResume of each fragment I try to de highlight all other tab bars and highlight the current tab bar again. I access the bottomNavigationMenu from each fragment and do this. Not sure if this is the right way to handle it but it works atleast.

Veepee
  • 1
  • 1