如何取消活动或从Dialog调用finish()? [英] How to kill activity or call finish() from Dialog?
问题描述
我有以下代码:
public void promptUserToSaveModifications() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(getString(R.string.str_desea_guardar_antes_salir))
.setPositiveButton(getString(R.string.msj_si), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
if (valoresGenerales.options.op_timer_oper_edicion_ot == 1 && valoresGenerales.options.Edi_bas_ot_RegistroHoper_boton_ini_ter_trabajo == 0) {
promptUserToFinishCurrentJob();
} else if (btnMnHoraInicioRegistroHoper.getText().toString().equals(getString(R.string.str_edit_correctivo_finalizar_trabajo))){
promptUserToFinishCurrentJob();
} else if (saveModifications()) {
if (!postProcessSave())
finish();
}
}
}).show();
}
private void promptUserToFinishCurrentJob() {
if (existsCurrentJob()) {
exist_inicio_hoper = true;
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(getString(R.string.str_trabajos_iniciados))
.setPositiveButton(getString(R.string.msj_si), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
toggleCurrentJob();
if (saveModifications()) {
if (!postProcessSave())
finish();
}
}
}).show();
}
}
@Override
public void finish() {
Intent i = new Intent();
i.putExtra("index", getIntent().getIntExtra("index", 0));
i.putExtra("top", getIntent().getIntExtra("top", 0));
setResult(RESULT_OK, i);
super.finish();
}
问题是,如果它通过 promptUserTosaveModifications()
方法中的第二个 else if
输入 saveModifications()
,它将执行完成活动.但是,如果通过第一个 else if
进入 promptUserToFinishCurrentJob()
并在其中进入 saveModifications()
,则不会.完成活动...您知道为什么以及如何完成该活动吗?
The thing is that if it enters into the saveModifications()
by the second else if
in the promptUserTosaveModifications()
method, it does finish the activity. But, if by the first else if
it enters into promptUserToFinishCurrentJob()
and inside this one it enters into saveModifications()
,, it doesn't finish the activity... do you know why and how can I finish it?
谢谢!
推荐答案
您需要活动上下文来完成活动.尝试
You need the activity context to finish an activity. try
getApplicationContext().finish();
或
this.finish();
如果您想在警报对话框中完成活动,请分段使用
if you want to finish activity from an alert dialog in fragment the use
getActivity().finish();
这篇关于如何取消活动或从Dialog调用finish()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!