views:

806

answers:

2

Hello,

Let's suppose I have an applet running within a page in a browser. What happens when the browser is closed by the user?

Is the applet notified so that it can perform some kind of close action on its side (closing connections opened to a server, cleaning static variables, ...)?

Also, I assume the same behavior would apply for a page refresh or page navigation (instead of browser close). The browser remains opened but the applet is gone. Although when you close the browser you also close the JVM so I'm unsure at this point.

Thanks, JB

+1  A: 

Yes, the destroy() method should be called before the browser unloads the object.

destroy() is the last of four "life-cycle methods" of the Java applet (the others are init(), start(), and stop() ). They're actually called at different times depending on your browser and virtual machine. If you'd like to know exactly when each is called, implement each method within your applet, and System.out some feedback.

Ideally, destroy() should be called by the environment, and should only be called once. If it seems like destroy() is not being called, you might declare a public finalize() method, which calls destroy. You could also try to call destroy() from javascript as the window object unloads, but again, be sure that you're not calling destroy() unnecessarily.

public void finalize () {
    destroy();
}
keparo
+1  A: 

Most Of the times destroy will be called , but it dont get enough time to do required tasks in case of closing the window.

It gets enough time when refreshing , navigating with Backword <- and Forward ->