「MainActivity」と「LibraryActivity」という2つのアクティビティがあります。これら2つのアクティビティは、同じ「ボトムナビゲーションビュー」を使用しています。この場合、インテントが「LibraryActivity」を呼び出すときに、適切なアイテム(異なる色で強調表示)を選択できました。問題は、「LibraryActivity」の「Navigation View」アイテムの「onBackPressed()」を使用して「MainActivity」に戻ってきており、別の色で強調表示されていません。
以下は私のコードです:
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);
}
}
そのため、他のアクティビティが終了して現在のアクティビティが再開された後に再開したときに更新されます。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加