What happens when I click the X to close the app?

Hi!

When I click the X on my app, the window closes, but my program does not terminate because I have other stuff running. How can I detect that the X was clicked, i.e., what happens when I click the X?

Try adding something like this to your application:

[java]
@Override
public void destroy() {
((ClientNetworkManager) networkManager).closeConnection();
super.destroy();
System.exit(0);
}
[/java]

Not sure how much of that is really needed, but does the trick for me.

Well yes SystemExit is supposed to do the trick^^

But to be honest, at least on the Client side I do similar, nothing is more annoying than games that do nt terminate fast. Do all minimum necessary cleanups (eg sending a disconnect to the server, and then just terminate.)
The extra exit just gurantees that no matter what buggy stuff is running, it will terminate.

Pro tip: Put a try around everything and a finally around the exit, if for example the netowrkmanager throws an exeption on close it will prevent exiting else.

1 Like

Oh, good idea, I’ll add that

@loopies @Empire Phoenix

Shouldn’t have asked my question in quite that way. Your answer is understandable. :slight_smile:

However, I asked how I can detect that the X was clicked, not how to shut down the process. I don’t actually want to shut down the process. I take it destroy() runs? What’s the simplest way to detect it?

Hm, this is a bit odd:

Javadoc for SystemListener:


requestClose(boolean esc)
Called when the user requests to close the application.

Then in Application:


/**
* Internal use only.
*/
public void requestClose(boolean esc){
context.destroy(false);
}

Why is it odd?

@pspeed

Because given the Javadoc, my best guess would be to use requestClose as a hook to do what I need to do when the X is pressed. But then in Application “Internal use only.” makes it sound like I shouldn’t touch it.

So what should I do?

Stupid suggestion, so slap me if I am way off base, but something along the llines of AbstractAppState.cleanup would be a nice addition to SimpleApplication. Something that is called when the application exits cleanly or otherwise to allow users to kill threads, close executors, etc, etc.

@t0neg0d said: Stupid suggestion, so slap me if I am way off base, but something along the llines of AbstractAppState.cleanup would be a nice addition to SimpleApplication. Something that is called when the application exits cleanly or otherwise to allow users to kill threads, close executors, etc, etc.

You already have this, really. First, those kind of resources should be managed by app states anyway. And in the rare case they aren’t, override destroy(),

OP wants to know how to prevent the app from closing, though. And in that case, you override requestClose().

@tuffe said: @pspeed

Because given the Javadoc, my best guess would be to use requestClose as a hook to do what I need to do when the X is pressed. But then in Application “Internal use only.” makes it sound like I shouldn’t touch it.

So what should I do?

That means that you shouldn’t call it. Of course you can override it. I guess most people with the skills to be overriding parts of the normal stuff are also aware of what warnings they can ignore.

Bottom line: you can override this method to avoid closing the app… it’s what it’s there for… but you shouldn’t call that method yourself.

Edit: unless of course you are calling super.requestClose()

@pspeed Actually I want to close the app. But I need to do more than that, because the “app” is not the whole program. However, I don’t want to call System.exit, because I don’t want to shut down the JVM.

Also I don’t wanna have to write a new Java class, if I can help it (my project is in Clojure). Isn’t there a more dynamic way of hooking into the shut down sequence, or detecting that the X was clicked?

@tuffe said: @pspeed Actually I want to close the app. But I need to do more than that, because the "app" is not the whole program. However, I don't want to call System.exit, because I don't want to shut down the JVM.

Also I don’t wanna have to write a new Java class, if I can help it (my project is in Clojure). Isn’t there a more dynamic way of hooking into the shut down sequence, or detecting that the X was clicked?

AppState.cleanup() as already mentioned.

@pspeed said: You already have this, really. First, those kind of resources should be managed by app states anyway. And in the rare case they aren't, override destroy(),

OP wants to know how to prevent the app from closing, though. And in that case, you override requestClose().

Honestly, I wasn’t sure if this was the right thing to do… it’s what I’ve done anyways… But, then again… (Best Jim Kirk impression) I… also… extended Node…