Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Closing the activity after clicking positive button in alert dialog in android

Tags:

android

i have activity called "A" which displays the list of items.On clicking update button it shows the custom dialog (activity is showed in the back)with the list of items selected.When Order button inside custom dialog is clicked .

  1. Custom dialog disappears.
  2. progress dialog is showed(since order working is going on)
  3. then progress dialog is closed after the order work is done
  4. i ll show the alert dialog saying "Order confirmed"
  5. When i click the positive button in alert dialog i want to close the activity which in the back(ie Activity A).Want to move to Home screen
  6. How can i call the finish code inside the alertdialog positive button Onclicklistener()

Kindly help me to solve this problem.

noData.setPositiveButton("Ok", new DialogInterface.OnClickListener(){
    @Override
    public void onClick(DialogInterface dialog, int which) {
        dialog.cancel();
    }
});

How can i add the finish() after cancelling the alertDialog()

like image 669
user637503 Avatar asked Mar 22 '11 07:03

user637503


People also ask

Which method is used to close an alert dialog?

AlertDialog generally consists of the main title, the message, and two buttons, technically termed as a positive button and a negative button. Both positive and negative buttons can be programmed to perform various actions. By default, the negative button lets close the AlertDialog without any additional lines of code.

How do you set the functionality to the positive button for Alert dialog?

setPositiveButton() is used to create a positive button in alert dialog and setNegativeButton() is used to invoke negative button to alert dialog. Here setNeutralButton() is used to create a neutral cancel button.

How do you prevent a dialog from closing when a button is clicked?

If you wish to prevent a dialog box from closing when one of these buttons is pressed you must replace the common button handler for the actual view of the button. Because it is assigned in OnCreate(), you must replace it after the default OnCreate() implementation is called.

How do I close dialog on Android?

You can use the methods cancel() or dismiss() .


2 Answers

You'll need something like this:

noData.setPositiveButton("Ok",
    new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            // Finish activity
            finish();
        }
    });

Just adding finish() inside the onClick should do the trick.

like image 126
Philio Avatar answered Oct 30 '22 11:10

Philio


See the code below (replace MyActivity with the name of your activity that this code is living in):

noData.setPositiveButton("Ok", new DialogInterface.OnClickListener(){
    @Override
    public void onClick(DialogInterface dialog, int which) {
        MyActivity.this.finish();
}
});

Adding this is very important!

like image 28
wnafee Avatar answered Oct 30 '22 12:10

wnafee