Android返回键按下时Navigation Drawer选中状态不更新问题求助
Hey there! I see the issue you're facing—when navigating back from Fragment B to A via the back button, your navigation drawer's selected state doesn't update. Let's fix that together.
What's Causing the Problem?
Right now, you're correctly updating the selected item and marking it as checked when tapping a drawer menu item (in onNavigationItemSelected), but when you press back, you're just popping the fragment back stack without syncing that change to the navigation drawer. The drawer has no way to know which fragment is now visible.
Step-by-Step Solution
We'll add a back stack listener to detect when fragments are popped, then update the drawer's selected state based on the currently visible fragment.
1. Add a Back Stack Listener
In your onCreate method, register a listener for back stack changes. This will trigger whenever the back stack is modified (like when you press back):
@Override protected void onCreate(Bundle savedInstanceState) { // ... existing initialization code ... // Initialize navigation drawer drawerLayout =(DrawerLayout) findViewById(R.id.drawer_layout); navDrawer =(NavigationView) findViewById(R.id.menu_drawer); navDrawer.setNavigationItemSelectedListener(this); drawerToggle =new ActionBarDrawerToggle(this,drawerLayout, toolBar, R.string.drawer_open, R.string.drawer_close); drawerLayout.setDrawerListener(drawerToggle); drawerToggle.syncState(); selectedItem =savedInstanceState ==null?R.id.nav_item_1 :savedInstanceState.getInt("selectedItem"); // Add back stack listener HERE getSupportFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() { @Override public void onBackStackChanged() { updateNavigationSelection(); } }); // Set initial checked item navDrawer.setCheckedItem(selectedItem); }
2. Create a Method to Update Drawer Selection
Add this helper method to your MainActivity. It checks the currently visible fragment and updates the drawer's selected item accordingly:
private void updateNavigationSelection() { Fragment currentFragment = getSupportFragmentManager().findFragmentById(R.id.main_content); // Map each fragment to its corresponding drawer item ID if (currentFragment instanceof HomeFragment) { selectedItem = R.id.nav_item_1; } else if (currentFragment instanceof MicrosoftDskFragment) { selectedItem = R.id.nav_item_2; } else if (currentFragment instanceof GoogleDskFragment) { selectedItem = R.id.nav_item_3; } else if (currentFragment instanceof AppleDskFragment) { selectedItem = R.id.nav_item_4; } else if (currentFragment instanceof OthersDskFragment) { selectedItem = R.id.nav_item_5; } else if (currentFragment instanceof MobiSpecsFragment) { selectedItem = R.id.nav_item_6; } // Update the navigation drawer's checked item navDrawer.setCheckedItem(selectedItem); }
3. Minor Adjustment to Initial Fragment Setup
Make sure your initial HomeFragment load also sets the correct drawer state. You already have the initial fragment transaction, so adding the initial check (done in step 1 with navDrawer.setCheckedItem(selectedItem)) covers this.
How This Works
- The back stack listener detects whenever you press back (or modify the back stack in any way).
- The
updateNavigationSelectionmethod checks which fragment is currently displayed and maps it to the correct drawer item. - The drawer's checked state is updated to match the current fragment, so it stays in sync whether you navigate via the drawer or the back button.
Give this a test—now when you press back from Fragment B to A, the drawer should automatically select the Home item again!
内容的提问来源于stack exchange,提问作者Rohith Rebel

