Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Confusion about Applet's life cycle.

 
Qunfeng Wang
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
# 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.
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic