File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

Win a copy of Head First Android this week in the Android forum!
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: 434
# 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: 42956
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 ]
Qunfeng Wang
Ranch Hand

Joined: Jan 28, 2005
Posts: 434
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.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: Confusion about Applet's life cycle.
jQuery in Action, 3rd edition