无法关闭android中的对话框中的应用程序
问题描述:
我想在应用程序中添加一项功能。我想关闭应用程序,而我点击退出按钮,但我的按钮是在对话框,所以当我尝试使用完成()是不会做同样的。我只想关闭应用程序,请帮助。对于同一无法关闭android中的对话框中的应用程序
if (v.getId() == R.id.imgLogout) {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(getContext());
alertDialog.setMessage("Are you sure you want to exit?");
alertDialog.setPositiveButton("YES", new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// session.logoutUser();
finish();
}
});
//但完成
//代码没有在对话
答
首先在对话框类中通过调用者活动的背景下说MainActivit.class方面
现在首先关闭对话框
//以免窗口泄漏作为破坏它的上下文活动也会消失。
dialog.dismiss();
然后
((Activity) context).finish();
答
首先关闭所有已暂停活动工作。比你可以关闭应用程序。你记得这个活动是最后一次。
if (v.getId() == R.id.imgLogout) {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(getContext());
alertDialog.setMessage("Are you sure you want to exit?");
alertDialog.setPositiveButton("YES", new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
+0
finish()不工作在对话框 –
+0
如果使用片段比使用context.finish(); –
答
一个非常简单的方法来关闭程序是:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
所以你的情况试试这个:
if (v.getId() == R.id.imgLogout) {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(getContext());
alertDialog.setMessage("Are you sure you want to exit?");
alertDialog.setPositiveButton("YES", new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// session.logoutUser();
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
}
});
为什么是U起动活动?你需要使用**完成()**关闭应用程序。 – Raghavendra
我没有看到你使用'finish()' –
[如何以编程方式退出android应用程序]的可能重复(https://stackoverflow.com/questions/6330200/how-to-quit-android-application-programmatically) – Raghavendra