我想在用户单击 android 中的后退按钮时结束整个应用程序。目前它再次转到上一个打开的 Activity 。 我也尝试覆盖 onBackPressed() 但它不起作用。
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
onBackPressed();
}
return super.onKeyDown(keyCode, event);
}
enter code here
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
return;
}
最佳答案
试试这个,从将充当根的 Activity 启动您的应用程序。
public class Root extends Activity {
public void onCreate() {
super.onCreate();
handleIntent();
}
public void onNewIntent(Intent intent) {
setIntent(intent);
handleIntent();
}
private void handleIntent() {
boolean end = getIntent.getBooleanExtra("End");
if (end) {
finish();
} else {
Intent intent = new Intent(this, MyRealFirstActivity.class); // where this is the class that you want the user to see when they launch your app.
startActivity(intent);
}
}
public void onResume() {
super.onResume();
finish();
}
}
然后在您希望后退按钮结束应用程序的 Activity 中,执行以下操作:
public class LastActivity extends Activity {
public void onBackPressed() {
Intent intent = new Intent(this, Root.class);
intent.putExtra("End", true);
intent.addFlag(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
}
这样,在您启动应用程序然后点击后退按钮之间开始的所有 Activity 都将为您完成()。这基本上会从头到尾清理应用程序的状态,然后退出。
关于android - 当用户单击 android 中的后退按钮时,我想结束整个应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12870563/