MORE INFORMATION
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.
Using IE3 the above steps will produce the following output (as found in the javalog.txt file):
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