Joven Pua
Joven Pua

Reputation: 59

How to navigate to a dialog, and block code execution while waiting for the result from dialog?

I want a function in a class A that when called, will navigate (using navigation component) to a dialog, but there are more lines of code BELOW the navigation to be executed which I dont want to execute until I receive a result (let's say a boolean) from the dialog. How would I do that?

My main issue is trying to block execution of code after the navigation logic, not how to navigate to the dialog.

Specifically, I want to navigate to the dialog that has a button, that after a fixed amount of time or pressing the button, navigates back to the function in class A and executes the rest of the code.

I heard that Kotlin coroutines would be my solution (not sure because I don't use Kotlin), but I need a solution strictly in Java

Upvotes: 0

Views: 109

Answers (1)

Grela
Grela

Reputation: 106

You need to use a callback.

First, split the function, one function to open the dialog, another one to handle the result. Then, in the dialog logic, set the boolean and notify the original activity with a listener. Finally, that listener executes the second function.

The listener can be notified by pressing the button or after X time passed with the dialog opened.

Upvotes: 1

Related Questions