question

Edster Skelter avatar image
Edster Skelter asked

Particulars of HTML5 history popstate event on amazon fire devices

After several hours of debugging an HTML5 app in both FireTV and FireTVStick devices, we figured we'd post what we've leaned here about history handling in the mentioned devices for future reference: It's worth noting that our history handling works seamless across the following platforms: WebOS, SmartTV 2014, Tizen, Android (Cordova + OS Webview), Android (Cordova + Crosswalk Webview)... but when it came to fireTV devices, the navigator never triggered a "popstate" event regardless of how many states were pushed. The history handling (from Amazon's docs and demos) is straight forward: window.addEventListener("load", function () { window.addEventListener("popstate", function () { // do something console.log('popped'); }); }); But no popstate ever gets dispatched events when the back button is pressed. Long story short, after countless hours of debugging, we realized that Amazon's documentation ( https://developer.amazon.com/public/solutions/platforms/webapps/faq#How%20can%20I%20customize%20the%20Amazon%20Remote%20Back%20button%20behavior%20for%20a%20web%20app%3F) reported that "The Back button does not raise a standard key event". Well, nothing could be farther from the truth. A simple snippet: window.addEventListener("keydown", function (evt) { console.dir(evt) }); proved that not only the following standard key event is raised when the back button is pressed: KeyboardEvent altKey: false bubbles: true cancelBubble: false cancelable: true charCode: 0 keyCode: 27 keyIdentifier: "U+001B" keyLocation: 0 type: "keydown" view: Window which: 27 ... but that it is also a "cancelable" event, and when cancelled ( e.g. event.preventDefault() ), the navigator does not raise the expected "popstate" event. Our application use an abstraction layer that monitors all key events, and dispatches other events (dpadup, etc...) based on which key is pressed. This allows us to achieve portability across platform. The module also happens to be preventing the default on certain keys (enter, tab, esc. etc...). Since user agents default handling vary slightly for these keys,, the application handles them itself uniformly instead. Thus since the fireTV devices trigger a standard cancelable keyboard ESC. event for the back button AND Amazon documentation reports otherwise... we ended up doing several hours of debugging on the Amazon web app tester. I hope this helps.
fire tv
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Ross@Amazon avatar image
Ross@Amazon answered
Thanks for sharing your insight on this issue.
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Joseph Shelby avatar image
Joseph Shelby answered
This may be 'new' to 5.0. My app has definitely been getting popstate events (and no ESC key events) for the past year on FireOS 4.x. One of my sticks is still on 4 and still works that way. However, on 5.0, my app is now getting 'back' events twice, so it seems I too am getting that ESC key press for the back button, which I capture and react for Chrome app and on the Web. Thus, I get a popstate event from the remote, AND the ESC keycode (not sure yet which order) which causes my app to pop 2 states of history and often exit.
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Joseph Shelby avatar image
Joseph Shelby answered
my workaround for now, in my switch statement for the key codes: if (keyCode === 27 && window.isFireTV) { return true; } // else I explicitly popState like I have to for the game controller 'b' button where isFireTV is defined by a check against the userAgent, "if (ua.indexOf('; AFT') >=0)" within an if that already checks for Amazon platform strings. So the escape key press does nothing but return true (allowing the event to propagate) and the real popState happens as expected and as it does on FireOS 4.
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Joseph Shelby avatar image
Joseph Shelby answered
I just posted elsewhere: I'm also getting a keycode (18) for the menubutton, instead of (and not in addition to) the expected menubutton event. Only 5.0. 4.x is still working as originally specified in sending me the menubutton event, though I can no longer find any documentation on that event at this time.
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Joseph Shelby avatar image
Joseph Shelby answered
I think I figured out what changed. With 5.0, the Fire TV and Fire Stick can connect to bluetooth-based keyboards, according to http://www.aftvnews.com/everything-you-need-to-know-about-the-upcoming-fire-os-5-software-update-for-the-1st-gen-fire-tv-and-fire-tv-stick/#bluetooth As such, I theorize that the system could not block those original keycodes (18, 27) anymore because there is a legitimate need for those keys to be passed through to the application. Just a thought. Joe
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Joseph Shelby avatar image
Joseph Shelby answered
The issue of the back-button sending an ESC 27 to the app, as well as the popstate event, also affects Fire Tablets running the new 5.0 update.
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Levon@Amazon avatar image
Levon@Amazon answered
Thanks Joseph and Edster, I have forwarded your comments to the Web team. Thanks!
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Levon@Amazon avatar image
Levon@Amazon answered
Thanks for your patience. I have replied to a related post here: https://forums.developer.amazon.com/forums/thread.jspa?threadID=11374 There were a combination of changes that were made to both the system WebView and the Web App Tester around capturing system keys. Unfortunately the documentation changes have not yet completed and therefore are not up-to-date. You are correct, and the changes to pass these system keycodes through was made very recently. Just to reiterate a point made in the related forum thread, to see this change in a live app you will need to submit a new app or re-submit your existing application in order for us to rebuild the APK with updated code. The latest version of Web App Tester will also have the change. As for the back handling not working as described when using the push/pop history states, let me follow-up with the team and get more clarity and I will respond to this thread when I have additional info. Thanks!
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.