Android onBackPressed返回键异常:多级菜单返回直接退出应用求助
Alright, let's break down your issue first: your current onBackPressed() only handles the search view and a double-tap exit for the home screen, but it completely ignores the state of your open menus. When you open two menus, the system has no record of those menus being part of the back stack, so pressing back just exits the app instead of navigating back through the menus one by one.
Here's a step-by-step solution to fix this:
1. Track Open Menus with a Stack
First, we need to keep track of the order in which menus are opened. A Stack is perfect for this because it follows a "last-in, first-out" (LIFO) pattern—exactly how back navigation should work.
Add this member variable to your Activity:
// Track the order of open menus private Stack<View> menuStack = new Stack<>();
2. Update Menu Open/Close Logic
Every time you open a menu, push its view onto the stack. If users can manually close menus (not just via the back button), make sure to pop it from the stack then too to keep the state consistent.
Example code for opening menus:
// Call this when opening your first menu public void openFirstMenu() { View firstMenu = findViewById(R.id.your_first_menu_id); firstMenu.setVisibility(View.VISIBLE); menuStack.push(firstMenu); } // Call this when opening your second menu public void openSecondMenu() { View secondMenu = findViewById(R.id.your_second_menu_id); secondMenu.setVisibility(View.VISIBLE); menuStack.push(secondMenu); } // If users can close menus via a close button, add this public void closeMenu(View menuView) { menuView.setVisibility(View.GONE); if (!menuStack.isEmpty() && menuStack.peek() == menuView) { menuStack.pop(); } }
3. Modify onBackPressed() to Handle Menu Navigation
Now update your back button logic to prioritize the search view first, then navigate back through the menu stack, and only fall back to the home screen exit logic when all menus are closed.
Here's the revised code:
@Override public void onBackPressed() { MaterialSearchView searchView = findViewById(R.id.search_view); // 1. Handle open search view first if (searchView.isOpen()) { searchView.closeSearch(); return; } // 2. Navigate back through open menus if (!menuStack.isEmpty()) { View currentOpenMenu = menuStack.pop(); currentOpenMenu.setVisibility(View.GONE); return; } // 3. Handle home screen double-tap exit (your original logic) if (count == 0) { count++; // Add your exit prompt here, e.g.: Toast.makeText(this, "Press back again to exit", Toast.LENGTH_SHORT).show(); // Reset count after 2 seconds so the prompt doesn't stay active forever new Handler(Looper.getMainLooper()).postDelayed(() -> count = 0, 2000); } else { // Actually exit the app when double-tapped super.onBackPressed(); } }
Key Notes
- Fragment Menus? If your menus are Fragments instead of Views, replace the
Stack<View>with calls togetSupportFragmentManager().popBackStack()when handling back presses, and useaddToBackStack(null)when launching each menu fragment. - Consistency: Always update the menu stack whenever a menu is opened or closed manually—this prevents mismatches between the stack state and actual menu visibility.
内容的提问来源于stack exchange,提问作者rithika gujjula

