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

时间:2018-05-31 05:40:19

标签: java android android-intent bottomnavigationview onbackpressed

我有两个名为'MainActivity'和'LibraryActivity'的活动。这两项活动使用相同的“底部导航视图”。在这种情况下,我设法选择正确的项目(用不同的颜色突出显示)时 意图称为'LibraryActivity'。问题是回到'MainActivity',使用'LibraryActivity'底部的'onBackPressed()',导航视图'项目不会以不同的颜色突出显示。

以下是我的代码:

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

1 个答案:

答案 0 :(得分:2)

拿这段代码,

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

在其他活动完成并恢复当前活动后,它会在恢复时更新。