ProgrAmmar
ProgrAmmar

Reputation: 3095

android: Exiting App(Activity) on dialogue box close

I have an activity which opens a dialogue box to sign up or log in. What i want is that if i press the back button not only the dialogue box but also the activity should exit. How can i achieve this?

Upvotes: 0

Views: 2384

Answers (3)

Sprigg
Sprigg

Reputation: 3319

You can set an setOnCancelListener Listener to your dialog and simply call finish()

Upvotes: 1

waqaslam
waqaslam

Reputation: 68177

Use setOnCancelListener to call finish() when back-key is pressed on dialog

//for example
new AlertDialog.Builder(this)
.setOnCancelListener(new OnCancelListener() {

    @Override
    public void onCancel(DialogInterface dialog) {
        finish();   //to finish Activity on which dialog is displayed
    }
})
...

Upvotes: 1

Shrikant Ballal
Shrikant Ballal

Reputation: 7087

public void onBackPressed() {

       final Builder builder = new Builder(this);
        builder.setTitle(R.string.caption);
        builder.setMessage("Do you really want to exit?");
        builder.setPositiveButton(android.R.string.ok, new OnClickListener() {
            @Override
            public void onClick(final DialogInterface dialog, final int which) {
                     YourActivity.this.finish();
                     dialog.dismiss();    
            }
        });
        builder.setNegativeButton(android.R.string.cancel,
                new OnClickListener() {
                    @Override
                    public void onClick(final DialogInterface dialog,
                            final int which) {
                                                  dialog.dismiss();
                    }
                });
        final AlertDialog dialog = builder.create();
        dialog.show();
}

This should solve your problem :)

Upvotes: 0

Related Questions