我按照某片文章的方式在我的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);
}
}}
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);
}
}}
解决方案 »
- 关于onActivityResult和finishActivity(int requestCode)问题
- 程序装载完不自动启动什么原因?有谁遇到过。
- [急]关于android模拟器和DDMS进行交互的问题
- LayoutInflater的inflate方法
- AlertDialog Button
- activity跳转 请大神们帮忙看看
- SharedPreferences存储数据丢失的问题
- Android如何建立一个后台监听的Service来得到屏幕的点击事件
- Android如何清理缓存,如何删除data/data文件里面的数据、文件夹
- android无线热点状态监听
- 请问android怎么监听软键盘的event?
- 求助:怎么能让TextView内容自动滚屏
@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