Android返回键关闭应用而非返回HomeActivity问题排查
Hey there, let's break down what's going wrong here and fix it step by step! The core issue is that you're using the wrong method to switch between screens, which breaks Android's Activity stack behavior—so the system doesn't know to go back to HomeActivity when you press the back button.
1. 核心错误:用setContentView()代替Intent启动Activity
In your code, you're using setContentView(R.layout.activity_detail) to "switch" pages. But this only replaces the layout of the current Activity (HomeActivity) instead of launching a new DetailActivity instance. Android keeps track of open Activities in a stack—since you never actually started a new DetailActivity, pressing back has no previous Activity to return to, so it closes the app.
2. 修复HomeActivity的页面跳转逻辑
Replace all instances of setContentView(R.layout.activity_detail) (and other layout switches) with proper Intent-based navigation. Here's how to fix the key parts:
修复按钮点击事件
btn1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 用Intent启动DetailActivity Intent intent = new Intent(HomeActivity.this, DetailActivity.class); startActivity(intent); } }); btn2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(HomeActivity.this, DetailActivity.class); startActivity(intent); } });
修复NavigationView的菜单跳转
Also, you missed a break in the menu_governance case (which would cause unexpected behavior), so fix that too:
case R.id.menu_governance: { Intent intent = new Intent(HomeActivity.this, DetailActivity.class); startActivity(intent); break; // 必须加break,否则会执行后续case }
修复拼写错误的方法
You have a typo in onOptionItemSelected—it should be onOptionsItemSelected (note the extra "s"). This fixes the drawer toggle button behavior:
@Override public boolean onOptionsItemSelected(MenuItem item){ if (navToggle.onOptionsItemSelected(item)){ return true; } return super.onOptionsItemSelected(item); }
3. 修复DetailActivity的返回逻辑
Your DetailActivity doesn't need to inherit from HomeActivity—this adds unnecessary complexity. Instead, inherit directly from AppCompatActivity, and let Android's default back behavior do the work.
Here's the corrected DetailActivity code:
package com.iridh.collegeapp; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; public class DetailActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_detail); Button b3 = findViewById(R.id.btn_back); b3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { finish(); // 销毁当前Activity,自动回到HomeActivity } }); } // 不需要重写onBackPressed!默认行为就是返回上一个Activity // 如果你想自定义返回逻辑,只需要调用super.onBackPressed()即可: // @Override // public void onBackPressed() { // super.onBackPressed(); // 保持默认返回行为 // } }
关键要点总结
- Always use
Intent+startActivity()to launch new Activities—setContentView()only changes the current Activity's layout, not the screen. - Keep Activities independent—don't inherit from business Activities like HomeActivity unless you're creating a base class.
- Double-check method names (like
onOptionsItemSelected) for typos—small mistakes break functionality. - Android's default back button behavior works out of the box when you use proper Activity navigation.
内容的提问来源于stack exchange,提问作者Chetan Upare




