无法检查底部导航视图项目

Unable to checked Bottom Navigation View item

我有两个名为 'MainActivity' 和 'LibraryActivity' 的活动。这两个活动使用相同的 'Bottom Navigation View'。在这种情况下,我设法 select 正确的项目(用不同的颜色突出显示) 意图调用 'LibraryActivity'。问题是通过使用 'LibraryActivity' 底部的 'onBackPressed()' 回到 'MainActivity' 'Navigation View' 项目没有用不同的颜色突出显示。

下面是我的代码:

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();
    }

接受这段代码,

   Menu bottomMenu = bottomNavigationView.getMenu();
    for (int i=0; i<bottomMenu.size(); i++)
    {
        MenuItem bottomMenuItem = bottomMenu.getItem(0);
        bottomMenuItem.setChecked(true);
    }

并放在这里,

@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);
    }
}

以便在其他 activity 完成并恢复当前 activity 后恢复时更新。