views:

235

answers:

2

I noticed that if you're playing a song at http://listen.grooveshark.com/ and you hit the back button Flash is smart enough to keep on playing the music while navigating "back" inside the Flash application.

Is it possible to implement this sort of thing using Java Applets, or do Applets alway shut down when you navigate away from the page (even though the resulting page contains the same applet)?

+2  A: 

http://java.sun.com/docs/books/tutorial/deployment/applet/lifeCycle.html

When the user leaves the page, for example, to go to another page, the browser stops and destroys the applet. The state of the applet is not preserved. When the user returns to the page, the browser intializes and starts a new instance of the applet.

That being said, what you could do is save the state to the server when the applet is stopped and then restore the state from the server when it starts again. If you make it a signed applet it should be able to save the state locally.

TofuBeer
So it sounds to me like it's not possible to recreate this application in Java because clearly it's not acceptable for the music to stop playing as you move from page to another. Is there a way to hook the browser buttons instead so the location bar looks like it's changing pages even though it's not?
Gili
Do the navigation insde the applet rather than a new page for each thing. An applet is able to be more heavyweight than a web page.
TofuBeer
+2  A: 

Looks like grooveshark is being tricky with the URL fragment. They store the search after the # fragment delimiter in the URL, e.g. do a search for ween, and you get this URL

http://listen.grooveshark.com/#/search/songs/?query=ween

Then do a search for bungle and the URL changes to

http://listen.grooveshark.com/#/search/songs/?query=bungle

If you click the back button in your browser, the URL changes to the previous "ween" one, but the browser remains on the same page, because everything before the fragment identifier is the same. There's some javascript that's detecting the changed fragment and updating the UI accordingly.

You could probably do something like this with an applet, but it seems better suited to javascript. The good news is, your applet is going to be cached by the browser, so if you do switch to a different page the applet loading will happen quickly.

Sam Barnum
Exactly, every rich application reacts the same way when you navigate away from a page.
Pool
Look up SWFAddress if you want to better understand how they're doing that in Flash. It takes advantage of the fact that going "back" to a previous anchor tag doesn't necessarily force a page reload. You can do that with JS as well. There are some caveats vis-a-vis SEO, but it's definitely doable.
Myk