This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Applets and the fly likes Confusion about Applet's life cycle. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » Applets
Bookmark "Confusion about Applet Watch "Confusion about Applet New topic

Confusion about Applet's life cycle.

Qunfeng Wang
Ranch Hand

Joined: Jan 28, 2005
Posts: 433
# init: This method is intended for whatever initialization is needed for your applet. It is called after the param attributes of the applet tag.
# start: This method is automatically called after init method. It is also called whenever user returns to the page containing the applet after visiting other pages.
# stop: This method is automatically called whenever the user moves away from the page containing applets. You can use this method to stop an animation.
# destroy: This method is only called when the browser shuts down normally.

The example in Java Tutorial seems don't work that way. When I first launch this example via:
It prints:
as expected. Then I switch to another window(ex. my NetBeans IDE), then switch back to the browser. It doesn't print:
Maybe the switch action should be invoked within browser. So I click another link from the Applet page, then go back. It prints:
preparing for unloading...
It's not what I expected either. Is my understanding of these methods wrong? How to let that example print stopping... starting... by turns?


To be or not to be. It's a question.
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42022
stop will be called if the window containing the applet is closed, or if the page is replaced by a different one, not if the page is merely switched to the background.

You'll rarely see stop and start without an intervening init call, because applet instances are not cached between page views. This page implies otherwise, but I think the sentence "Some browsers reload the applet when you return to its page." actually means "All browsers reload the applet when you return to its page."
[ June 18, 2007: Message edited by: Ulf Dittmer ]

Ping & DNS - my free Android networking tools app
Qunfeng Wang
Ranch Hand

Joined: Jan 28, 2005
Posts: 433
Thank you for clarifying me.

Originally I want to use the feature I thought to improve the performance. My applet poll a device to update status every 1 second. When the applet runs into background, it's better to stop polling. I have to find another way now.
GeeCON Prague 2014
subject: Confusion about Applet's life cycle.