Use stage.setOnCloseRequest
to perform actions before your stage closes. Like
public void start(Stage primaryStage)
{
// do all the regular stuff.
primaryStage.setOnCloseRequest(E -> {
// perform actions before closing
});
}
Overriding the stop()
method in your Application class is also a valid method to add a shutdown hook. Though from experiance it isn't always called when your Application shuts down.
Lastly if you want to shutdown your entire Application, please use Platform.exit()
(static method). The stage.close()
method is equal to stage.hide()
, meaning only the window will be hidden, the Application will not shutdown.
EDIT1:
If you want to execute code when your stage is hiding. Use the following snippit.
primaryStage.setOnHiding(E -> {
// perform actions upon hiding.
});
EDIT2:
If you want a first Stage to wait for a second Stage, and continu after the second Stage is closed use secondaryStage.showAndWait()
. This way you just close the secondary Stage if you want to continu with the execution on the first Stage.