AlertBox调用Intent而不点击肯定按钮

时间:2012-04-01 20:05:04

标签: android android-intent alertdialog android-alertdialog

很抱歉再次提出这个问题,但我在stackoverflow和其他Android组上尝试了几个解决方案,但没有一个能为我工作。

在onBackpressed活动中,我想显示一个警告框,询问用户“退出应用程序?”如果用户点击“是”,我想转到我的应用程序的启动器(主页)活动,请点击“否”按钮停留在同一页面上。 问题是,在显示警告框后,我会在按下“是”按钮之前自动重定向到启动器(主页)活动

Plz帮助

CODE:

public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
Log.i(TAG, "BACK PRESSED");
AlertDialog.Builder renameDialog = new AlertDialog.Builder(MainMenu.this);
renameDialog.setTitle("Warning");
renameDialog.setMessage("Do you want to logout?");
renameDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
launchIntent();
}
 });

renameDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
arg0.cancel();       
}
 });
renameDialog.show();
}

 private void launchIntent() {
Log.i("positive button","pressed");
Intent i=new Intent(MainMenu.this,LoginActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
Log.e("from main menu","calling login");
startActivity(i); 
}

2 个答案:

答案 0 :(得分:3)

您需要在正面按钮的点击监听器内移动super.onBackPressed();,因为它只会完成正在运行的活动。或者,如果您愿意,只需对此行进行注释(排除它),因为当前逻辑显示您在单击“正”按钮时尝试启动新活动。

答案 1 :(得分:0)

不要调用super.onBackPressed()!