我正在开发一个android应用程序,当要返回没有可用视图返回的状态时,我想在按下时退出该应用程序。在此之前,应按返回上一个视图。我怎样才能做到这一点。
我需要的
我的密码
@Override
public void onBackPressed() {
DialogInterface.OnClickListener dialogInterface = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
break;
case DialogInterface.BUTTON_NEGATIVE:
}
commonVariable.setSteps(0);
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(
MainActivity.this);
builder.setMessage("Are you sure to exit the app?")
.setPositiveButton("Yes", dialogInterface)
.setNegativeButton("No", dialogInterface).show();
}
}
解决方案
@Override
public void onBackPressed() {
if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
getSupportFragmentManager().popBackStack();
} else {
DialogInterface.OnClickListener dialogInterface = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
break;
case DialogInterface.BUTTON_NEGATIVE:
}
commonVariable.setSteps(0);
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(
MainActivity.this);
builder.setMessage("Are you sure to exit the app?")
.setPositiveButton("Yes", dialogInterface)
.setNegativeButton("No", dialogInterface).show();
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句