Harry Adams
Harry Adams

Reputation: 487

Run a function after dialog box is closed

I have a custom dialog box that I am displaying in the following way:

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.dict_add_word_dialog_box);
    ok = findViewById(R.id.dictDialog_confirmButton);
    cancel = (Button) findViewById(R.id.dictDialog_cancelButton);
    ok.setOnClickListener(this);
    cancel.setOnClickListener(this);
}

This displays when tapping the Floating Action Button, via:

fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            DictCustomDialogBoxClass customDialog = new DictCustomDialogBoxClass(DictionaryActivity.this);
            customDialog.show();
            refreshRecyclerView();
        }
    });

I'd like the refreshRecyclerView(); to run only once the user has pressed the OK button on the dialog box. How would I go about doing this?

In addition, how would I go about running it only if the user has pressed OK, and not Cancel?

Upvotes: 0

Views: 825

Answers (2)

Daniel T-D
Daniel T-D

Reputation: 675

Why not add a listener to your custom dialog?

var listener: Listener

public interface Listener {
    void onPositiveActionPressed();
}

Then in your custom dialog's ok.onClickListener you'd have the below;

if(listener != null) {
    listener.onPositiveActionPressed();
}

Finally;

DictCustomDialogBoxClass customDialog = new DictCustomDialogBoxClass(DictionaryActivity.this)
customDialog.listener = self

Of course having implemented DictCustomDialogBoxClass.Listener

Upvotes: 0

Nikos Hidalgo
Nikos Hidalgo

Reputation: 3756

Create a runnable with your refreshRecyclerView method in it:

Runnable r = new Runnable() {
        @Override
        public void run() {
            refreshRecyclerView();
        }
    }

then create a handler for that runnable:

Handler handler = new Handler();

inside your onClickListener for the ok button trigger the runnable by calling the following:

handler.post(r);

Upvotes: 1

Related Questions