aspose file tools*
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
Author

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:http://java.sun.com/docs/books/tutorial/deployment/applet/index.html
It prints:
initializing...
starting...
as expected. Then I switch to another window(ex. my NetBeans IDE), then switch back to the browser. It doesn't print:
stopping...
starting...
Maybe the switch action should be invoked within browser. So I click another link from the Applet page, then go back. It prints:
stopping...
preparing for unloading...
initializing...
starting...
It's not what I expected either. Is my understanding of these methods wrong? How to let that example print stopping... starting... by turns?

Thanks.


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

Joined: Mar 22, 2005
Posts: 42648
    
  65
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Confusion about Applet's life cycle.