apache / cordova-browser

Apache Cordova
Apache License 2.0
168 stars 85 forks source link

page lifecycle #79

Closed benitogf closed 5 years ago

benitogf commented 5 years ago

Bug Report

Problem

The cordova-android lifecycle clashes with the page lifecycle spec.

What is expected to happen?

There should be some integration between cordova lifecycle and the page lifecycle

What does actually happen?

The cordova even 'resume' name clashes with 'resume' of page lifecycle

Information

The browser pause event could be a proxy of freeze. currently pause will trigger even on a browser tab change, which makes sense on the mobile webview, but not so much on a browser.

Command or Code

The easiest way to test integration with page lifecycle that I found so far is with chrome://discards/ as stated here https://developers.google.com/web/updates/2018/07/page-lifecycle-api#testing-your-app-in-the-frozen-and-discarded-states

Environment, Platform, Device

Tested with cordova-android and cordova-browser

Version information

Cordova: 9.0

Checklist

janpio commented 5 years ago

The cordova-android lifecycle crashes

What's the connection to cordova-browser here?

benitogf commented 5 years ago

think that cordova-browser could use the page lifecycle events, as stated above a browser changing tabs shouldn't require a pause event imho

janpio commented 5 years ago

Ok, but what is crashing? Or did you mean "clash"? Even then I don't understand how something from cordova-android is relevant for cordova-browser? Or does browser implement the current stuff right now?

benitogf commented 5 years ago

in the browser resume and pause are called on visibility change (https://github.com/apache/cordova-browser/blob/master/cordova-js-src/platform.js#L35), think that it could further filter the trigger to actual lifecycle events like freeze and resume, the reason why I included the link to cordova-android docs is because I couldn't find a lifecycle specification for cordova-browser

Oh sorry I did meant "clash", updated the ticket

janpio commented 5 years ago

I am not even sure if there is any special lifecycle handling for cordova-browser - it probably comes from the browser itself I guess. But we will have to look into the code further here.

So your issue is more like a feature request to better integrate the page lifecycle API you linked to, and not about a problem or bug that currently exists?

benitogf commented 5 years ago

would consider it a bug rather than a new feature since there's already an implementation in cordova-browser but not sure

regardless of an integration with the page lifecycle API, a visibility change should trigger pause ?

my experience was that implementing lifecycle events works fine on android but made the application "pause" on every tab change when deployed to the browser

janpio commented 5 years ago

my experience was that implementing lifecycle events works fine on android but made the application "pause" on every tab change when deployed to the browser

Ah, now we are getting concrete. That is not normal browser behavior?

benitogf commented 5 years ago

That is not normal browser behavior?

Is not in my opinion, but open to different ideas

janpio commented 5 years ago

The easiest way to find out (and also later to test and verify any implementation in cordova-browser) would be a super simple Cordova app / index.html that outputs the events that are occuring either in the console or in a div on a page.

Could you create one of those and put them on GitHub? (Bonus points if you follow these instructions for a minimal reproduction repository: https://github.com/apache/cordova-contribute/blob/master/create-reproduction.md - because that will minimize the time we contributors have to spend to set this up.)

benitogf commented 5 years ago

Hello @janpio I got around this by checking the user agent before adding the event listener to filter the desktop browser which works even for the webview build (https://developer.chrome.com/multidevice/user-agent):

if (navigator.userAgent.match(/Android|iPhone|iPad|iPod/i)) {
   document.addEventListener('pause', () => {})
   document.addEventListener('resume', () => {})
}

I realize that the page lifecycle events are not implemented on other browsers and that some applications might depend on current behavior of cordova-browser events so closing this one for now, thanks