Android How to - Close Activity








Question

We would like to know how to close Activity.

Answer

/*from www .j  av a 2s .c om*/

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;

/**
 * Closes the activity.
 * 
 * @author Marcello de Sales (marcello.desales@gmail.com)
 * 
 */
class CloseActivityAction {

    public static void confirm(final Activity activity, String message, String positiveButton, String negativeButton) {
        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        builder.setMessage(message).setCancelable(false)
                .setPositiveButton(positiveButton, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        activity.setResult(Activity.RESULT_CANCELED);
                        activity.finish();
                    }
                }).setNegativeButton(negativeButton, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.cancel();
                    }
                });
        AlertDialog alert = builder.create();
        alert.show();
    }
}