Title: Unable to checked Bottom Navigation View item
Tags: java;android;android-intent;bottomnavigationview;onbackpressed
Question: I have two Activities called 'MainActivity' and 'LibraryActivity'. These two activities are using the same 'Bottom Navigation View'. In this case I have managed to select the correct item (highlighted with a different color) when
the intent calls the 'LibraryActivity'. The problem is coming back to the 'MainActivity' by using 'onBackPressed()' from 'LibraryActivity' bottom 'Navigation View' item is not highlighted with a different color.
Below is my code:
MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_navigation);
BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);
// used to highlight the correct item
Menu bottomMenu = bottomNavigationView.getMenu();
for (int i=0; i<bottomMenu.size(); i++)
{
MenuItem bottomMenuItem = bottomMenu.getItem(0);
bottomMenuItem.setChecked(true);
}
// item click listener
bottomNavigationView.setOnNavigationItemSelectedListener(
new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_library:
Intent intent = new Intent(getApplicationContext(), LibraryActivity.class);
intent.putExtra("NUM", "0");
startActivity(intent);
break;
}
return true;
}
});
LibraryActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_library);
context = this;
bottomNavigationView = (BottomNavigationView)findViewById(R.id.bottom_navigation);
BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);
// used to highlight the correct item
Menu bottomMenu = bottomNavigationView.getMenu();
for (int i=0; i<bottomMenu.size(); i++)
{
MenuItem bottomMenuItem = bottomMenu.getItem(1);
bottomMenuItem.setChecked(true);
}
// item click listener
bottomNavigationView.setOnNavigationItemSelectedListener(
new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.action_for_you:
onBackPressed();
break;
return true;
}
});
@Override
public void onBackPressed() {
super.onBackPressed();
Comment: @Jantzilla: regarding your edit, bolding whole sentences rarely makes them more readable. In general, bold is not needed at all in a readable piece of writing, with the exception of headings.

Here is the accepted answer: Take this code ,
Menu bottomMenu = bottomNavigationView.getMenu();
for (int i=0; i<bottomMenu.size(); i++)
MenuItem bottomMenuItem = bottomMenu.getItem(0);
bottomMenuItem.setChecked(true);
and place it here ,
@Override
protected void onResume() {
super.onResume();
Menu bottomMenu = bottomNavigationView.getMenu();
for (int i=0; i<bottomMenu.size(); i++)
MenuItem bottomMenuItem = bottomMenu.getItem(0);
bottomMenuItem.setChecked(true);
So that it gets updated when it resumes after some other activity finishes and the current activity is resumed.


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 1, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2018:00:05/31/18', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<java><android><android-intent><bottomnavigationview><onbackpressed>', 'Title': 'Unable to checked Bottom Navigation View item', 'ViewCount': 326, 'id': 50617348, 'user_AboutMe': '', 'user_AccountId': 11465133.0, 'user_DisplayName': 'Nuwan Withanage', 'user_Reputation': 383.0, 'user_Views': 156.0, 'user_id': 8403554.0}}