我按照某片文章的方式在我的activity中加入按下返回键弹出确认退出的对话框,单击确认后退出应用程序的方法。可是运行时程序并不会退出而是跳转到某个曾经访问到的activity中,究竟是哪里出问题了?package com.daizhen.gencalc;import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;public class Gen_calc_main extends Activity {
private Button newbutton = null;

private Button hisbutton = null; @Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.mainlist);
newbutton = (Button) findViewById(R.id.button1);
hisbutton = (Button) findViewById(R.id.button2);
newbutton.setOnClickListener(new newButtonListener());
hisbutton.setOnClickListener(new historyButtonListener());
}

class newButtonListener implements OnClickListener { @Override
public void onClick(View arg0) {
Intent startIntent = new Intent();
startIntent.setClass(Gen_calc_main.this, Gen_calc_addsingle.class);
Gen_calc_main.this.startActivity(startIntent);
}

}

class historyButtonListener implements OnClickListener { @Override
public void onClick(View v) {
Intent startIntent = new Intent();
startIntent.setClass(Gen_calc_main.this, Gen_calc_history.class);
Gen_calc_main.this.startActivity(startIntent);
}

} protected void onDestroy() {
super.onDestroy();
android.os.Process.killProcess(android.os.Process.myPid());
} public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK) {
new AlertDialog.Builder(this).setMessage("确认退出系统嘛?").setNegativeButton("取消", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {

}
}).setPositiveButton("确定", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
finish();

}
}).show();
return true;
} else {
return super.onKeyDown(keyCode, event);
}
}}

解决方案 »

  1.   

    怎么样才能执行到ondestroy方法?
      

  2.   

    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        finish();
                        
                    }
                }).show();
    这个,finish()是结束当前页面,和按返回键一样,肯定跳转到前一个页面了。
    你的意思应该是确定的时候退出整个程序把。
    我是把activity加进一个list,在需要程序结束时,finish所有list中的acitivity;
    参考这篇博文:http://blog.csdn.net/love__coder/article/details/6923579