https://kotlinlang.org logo
Title
l

Lukas K-G

08/20/2021, 9:50 AM
Is there a way to open e.g. a
Dialog
from the
onClick
of a
MenuItem
? Problem is that the
onClick
is not a composable function. 🤔
1
d

Dominaezzz

08/20/2021, 10:05 AM
Yes. Use a mutablestate boolean, set it to true in
onClick
then call Dialog if boolean is true.
l

louiscad

08/20/2021, 10:05 AM
Yes, by changing a state, and have the dialog be shown based on the value of that state.
Dominic, you won this time, by a thin margin!
:ninja:🏼 1
😄 8
l

Lukas K-G

08/20/2021, 10:06 AM
Thanks for the help. Appreciate! 🙏
y

Yan Pujante

08/21/2021, 6:34 AM
At the end of the day your question is very valid/good question because it directly relates to the fundamental shift in thinking between traditional UI programming (callbacks, etc...) and state based (like compose/react, etc..). In this instance, clicking a menu item needs to change the state of the application by stating: this specific dialog window is opened. Until dismissed (Ok/Cancel/...), it stays open. When dismissed, the state changes again. This is why you need to represent this state as suggested by a mutable state boolean (or enum if you have multiple types of dialog windows that could be opened,...). Then in your code you simply do:
if(dialogWindowOpened)
{
  // display dialog window
}
See example: https://github.com/JetBrains/compose-jb/tree/master/tutorials/Window_API_new#dialogs
👍 1
l

Lukas K-G

08/21/2021, 7:58 AM
Thanks @Yan Pujante, I also realized that this boils down to the very fundamentals of writing the UI in compose. Quite a big part of my application needed a rewrite after that, but it feels way better now. 🙂