I have the following code:
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();
}
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?
Thanks!
You need the activity context to finish an activity. try
getApplicationContext().finish();
or
this.finish();
if you want to finish activity from an alert dialog in fragment the use
getActivity().finish();