'How can I stop OnClickListener in the middle of execution?

I need to exit out of an OnClickListner event in the middle of its execution but, can't find a command to do so. Tried using break but that can only be used on for loops, I'm looking for a command that would just stop the execution process and not kill or finish the activity.

saveBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        amountEntered = (EditText) findViewById(R.id.amountEntered);
        Integer bal = Integer.parseInt(custBal);
        Integer enteredAmount = Integer.parseInt(amountEntered.getText().toString());

        // check if amount entered is more than the balance:
        if( enteredAmount > bal ){

            final AlertDialog.Builder alertDialog = new AlertDialog.Builder(MakePaymentActivity.this,R.style.MyAlertDialogStyle);
            alertDialog.setTitle("VERIFY AMOUNT");
            alertDialog.setMessage("Is entered amount correct?");
            alertDialog.setNegativeButton("NO",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        //Simply Exit here
                    }
                }
            );
        });

The other button (Positive Button) would just dismiss the dialog and continue saving.



Solution 1:[1]

just call alertDialog.dismiss() or alertDialog.cancel()

Solution 2:[2]

To stop a particular code from running in places, you use these keywords:

Place to stop Keyword
In a loop break; to stop the loop from running again and not running the remains code
continue; to stop the current round of the loop and proceed to the next one
In a while loop break; to stop the while loop from running again and not running the remains code
continue; to stop the current round of the while loop and proceed to the next one
In a method return; to stop the code from running further.

Here, we need to use the Second one from the table. This will stop the code from running further. Try this in your negative button:

alertDialog.setNegativeButton("NO",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        return; // <- Simply Exit here
                    }
                }
            );

Solution 3:[3]

I think you are looking for the return statement. It just returns from the current method, possibly with a return value, but that’s not what you’re looking for here.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Matias Elorriaga
Solution 2 Sambhav. K
Solution 3 bleistift2