INFO: When are Java Applet Methods init, start, stop, and destroy Called in IE3 Vs IE4 and Greater?ID: Q227210
|
This article describes exactly when the Java applet methods init(), start(), stop(), and destroy() are called in the different versions of Internet Explorer.
Internet Explorer versions 4.0 and later changed behavior with respect to these methods when compared to IE3. This article explains how to make your Java applet code run consistently in all versions of IE.
The change in behavior that occurs in IE4 and later was made mainly in order to save memory.
In IE3, the applet method init() is only called if the applet page isn't found in the cache and start() is always called when moving to the page. Furthermore, stop() is always called when leaving the page and destroy() is called only when the browser exited or the page was flushed out of the cache.
In IE4 and later, the applet methods init() and start() are always called when moving to an applet page, and stop() and destroy() are always called when leaving the page.
If an applet's init() function contains code that takes a long time to process and must always be available to the start() method, make this code static in order to extend its life beyond the time the applet is seen in the browser (important for IE4 and greater), speeding up subsequent visits to the applet. If the computation results are not needed instantly when the start() method is executed, it's recommended to start the long-running processing in another thread.
The following code demonstrates this issue and shows what needs to be done in order to mimic the IE3 behavior in IE4 and later. Running this code and inspecting the output from the various versions of IE clearly shows the difference in behavior since IE3. Note when running this code that the IE progress bar finishes much faster when revisiting the applet page compared to when it is first visited and the lengthy processing in the init() method is called.
Cut and paste the following code and save it to a file called Applet1.java. Compile this source code into a class file using Visual J++ 6.0 or the Microsoft SDK for Java:
import java.applet.*;
public class Applet1 extends Applet
{
//To keep track if init has been called
static Boolean m_initialized = Boolean.FALSE;
// Object that takes long time to create or populate
static String m_hugestring = "";
public void init()
{
synchronized (m_initialized)
{
System.out.println("Output from using init method");
if (m_initialized.booleanValue())
return;
System.out.println("Lengthy processing starting in init method");
// Do some lengthy processing to populate the m_hugestring
for ( int i=0; i < 10000; i++)
m_hugestring += "% ";
m_initialized = Boolean.TRUE;
}
}
public void stop()
{
System.out.println("Output from using stop method");
}
public void start()
{
System.out.println("Output from using start method: m_hugestring length = "
+ m_hugestring.length());
}
public void destroy()
{
System.out.println("Output from using destroy method");
}
}
Cut and paste the following code and save it to an HTML file, for example one called Applet1.htm:
<APPLET CODE=Applet1 WIDTH=190 HEIGHT=190>
Can't run an applet -- this browser doesn't support Java.
</APPLET>
Compare the output that results from running the above HTML code, moving off the applet page, moving back to the applet page, then moving off the applet page again and closing IE.
Output from using init method
Lengthy processing starting in init method
Output from using start method: m_hugestring length = 20000
Output from using stop method
Output from using start method: m_hugestring length = 20000
Output from using stop method
Output from using destroy method
Following the same steps using IE4 or greater will produce the following output (as found in the Java Console window):
Output from using init method
Lengthy processing starting in init method
Output from using start method: m_hugestring length = 20000
Output from using stop method
Output from using destroy method
Output from using init method
Output from using start method: m_hugestring length = 20000
Output from using stop method
Output from using destroy method
© Microsoft Corporation 1999, All Rights Reserved.
Contributions by Thomas Klevmar, Microsoft Corporation
For the latest Knowledge Base articles and other support information on Visual J++ and the SDK for Java,
please see the following pages on the Microsoft Technical Support site:
http://support.microsoft.com/support/visualj/
http://support.microsoft.com/support/java/
Additional query words:
Keywords : KbAwtPkg kbIE300 kbIE301 kbIE400 kbIE401 kbJavaVM kbIE302 kbIE401sp1 kbIE401sp2 kbGrpJava kbIE500
Version : WINDOWS:3.0,3.01,3.02,4.0,4.01,4.01 SP1,4.01 SP2,5.0
Platform : WINDOWS
Issue type : kbinfo
Last Reviewed: May 19, 1999