0

When the user has selected a contact, I am switching from the first fragment to the second fragment. My app switches to the second fragment, but the tab icon still shows the first fragment as selected.

This is the code I am using to switch to my second fragment.

Fragment fragment = new ItemSecondFragment(); getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, fragment).addToBackStack(null).commit();

Where R.id.content_frame is my frame layout.

How can I fix the above issue?

CaptainBli
  • 4,121
  • 4
  • 39
  • 58
  • 1
    Please show your complete code..with xml – Shubham Goel Jun 12 '17 at 11:55
  • TabLayout.Tab tab = tabLayout.getTabAt(someIndex); tab.select(); – MathankumarK Jun 12 '17 at 12:07
  • 1
    Follow this link to [stackoverflow](https://stackoverflow.com/questions/41501873/traversing-from-fragment-to-fragment-bottomnavigationview-icon-highlight) – Jeeva Jun 12 '17 at 14:14
  • Welcome to Stack Overflow! You will probably need to add the xml and a bit more of your code, if you wish to get further aid or help with your question. I reformatted and fixed the grammar to flow better in English. – CaptainBli Jun 12 '17 at 18:19

4 Answers4

2

I used the following code to set the tab icon when switching one fragment to another fragment

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


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

where R.id.tab_calls is my second fragment.

0

make your bottom tabbar public static in your activity and go to second fragment by bottom bar switcher like this @Override

public static BottomBar mBottomBar;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

}

and in your fragment

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    MainActivity.mBottomBar.selectTabAtPosition(2);

}

Meikiem
  • 1,876
  • 2
  • 11
  • 19
0

Try This Code.

private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.tab_home:
                    fragment= TabOne.newInstance();
                    item.setChecked(true);
                    break;
                case R.id.tab_email:
                    fragment= TabTwo.newInstance();
                    item.setChecked(true);
                    break;
                case R.id.tab_members:
                    fragment= TabThree.newInstance();
                    item.setChecked(true);
                    break;
                case R.id.tab_link:
                    fragment= TabFour.newInstance();
                    item.setChecked(true);
                    break;
            }
            final FragmentTransaction transaction = fragmentManager.beginTransaction();
            transaction.replace(R.id.content, fragment).commit();
            return false;
        }

    };`
Sanal
  • 418
  • 6
  • 17
0

You can use like this method :

View view = bottomNavigationView.findViewById(R.id.nav_button_two);
view.performClick();
Nikunj
  • 3,937
  • 19
  • 33
Praveenkumar
  • 31
  • 1
  • 10