How to highlight the item when pressed using BottomNavigationView between activities?

前端 未结 4 1257
情深已故
情深已故 2020-12-10 02:45

I have added Bottom Navigation View for my app but I need the Bottom Navigation View between activities instead of fragment so I have added this code to Java for all my 3 ac

4条回答
  •  盖世英雄少女心
    2020-12-10 03:10

    To anyone still looking for this, @javazian's answer of extending each activity is real overkill in my opinion.

    A much more succinct solution is to retrieve the nav menu and manually check the relevant menu item.

    Note in the example below, INSERT_INDEX_HERE needs to be replaced with the index of the menu item, (e.g. the menu item on the far left would have index 0).

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_connections);
    
        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
    
        // Ensure correct menu item is selected (where the magic happens)
        Menu menu = navigation.getMenu();
        MenuItem menuItem = menu.getItem(INSERT_INDEX_HERE);
        menuItem.setChecked(true);
    }
    

提交回复
热议问题