Open piterso opened 5 years ago
The onViewRemoved
method is listening to when the View is removed from the JavaFX scene-graph. Basically we use a listener on the sceneProperty of Node
.
For some reason this property isn't automatically reset to null
when the window is closed.
I managed to get it working with this code:
stage.setOnCloseRequest(windowEvent -> {
stage.getScene().setRoot(new VBox());
});
This explicitly changes the root-node of the scene so that the old root-node (your Window view) is removed from the scene. Maybe it's a good idea to wrap the setRoot
call in a Platform.runLater
because I don't know if there are any timing problems when you remove the root node before the window is actually closed as the callback is on "close request".
Thanks for your answer. Is your solution better than my code? Do my code can cause some problems?
No, your code also works fine. The advantage of your code is that you don't depend on strange behavior of JavaFX (like not resetting the scene). The disadvantage is that your ViewModel has to explicitly provide the stop method and you have to invoke this method manually in your app class which introduces a dependency form the app class to this specific viewModel. If later in time you implement another ViewModel which needs the same logic then you would also have to adjust your app class again.
Other then that I don't see any disadvantages in your code. Both solutions are trade-offs.
When i open new Window from my "Main Application Window" after closing new Window method onViewRemoved in my ViewModel is not invoked. I use also SpringBoot in my project and i try using @PreDetroy on both @Component scopes (prototype and singleton) - won't help. @PreDestroy is only invoked for singleton components and after whole application is closed but i want to listen for event when one of the windows in my application is closed.
This is the code that opens new window
I got ViewModel for TestView it looks like this:
After closing new Window based on TestView method onViewRemoved on TestViewModel is not invoked. It's pretty important for me to actually make it work.
I figure out that i can set on stage listener on close request like that:
I manage to make that work in that way:
is this the best way for doing this? onViewRemoved won't be invoked?
Do you have anny suggestions?