cordova-android icon indicating copy to clipboard operation
cordova-android copied to clipboard

In low memory, using overview button causes ondeviceready but not onresume

Open jmarshall-com opened this issue 3 years ago • 2 comments

Issue Type

  • [ x] Bug Report
  • [ ] Feature Request
  • [ ] Support Question

Description

I'm testing my app's onpause/onresume in low memory, so I've set "Don't keep activities" in Android's Developer Options. When this is set, and I use the "overview" button (aka app switcher, bottom right square) and choose my app again, onresume is never fired but ondeviceready is. Thus, there is no way to distinguish between the user starting the app (when I want the user to log in) and resuming it using the overview button (when I want to restore state).

Information

When my app calls the camera plugin in low memory, both ondeviceready and onresume are fired upon return.

When testing with normal memory conditions, all three events seem to work correctly, except: when using the overview button to reselect my app, onresume fires, which I thought would only happen if the phone actually ran out of memory, which is not the case here. (But maybe that detail is correct behavior?) In any case, of course, I need this to work in low-memory conditions.

Command or Code

I just put an alert() at the beginning of each of the ondeviceready and onresume handlers, indicating which handler is running.

Environment, Platform, Device

This is on a physical Pixel 2 XL running Android 11; I see the same results when running in an emulator.

Version information

Development machine is Linux (OpenSuSE Leap 15.1) Android 11 on Pixel 2 XL Cordova 10.0.0 cordova-plugin-camera 5.0.2 cordova-plugin-background-mode 0.7.3 cordova-plugin-autostart 2.3.0

I'm not using the last two yet, but my app will need them. I'm using many more plugins, but these are the only ones that seem relevant.

Checklist

  • [ x] I searched for already existing GitHub issues about this
  • [ ] I updated all Cordova tooling to their most recent version
  • [ x] I included all the necessary information above

jmarshall-com avatar Aug 11 '21 20:08 jmarshall-com

I'm guessing the cause of this behaviour (and why onResume works in the camera plugin) is due to this if statement:

https://github.com/apache/cordova-android/blob/dc4e065f619ce98d5c38211f5efd759d05959bec/framework/src/org/apache/cordova/CordovaWebViewImpl.java#L501-L506

It seems like it intentionally prevents the resume event if the app has been "resumed" without a pause event. This assumption is 6 years old... but I do understand your rationale and I think I generally agree that Cordova should fire resume whenever native does a resume. I wonder what would be the consequences of changing this... it definitely would be a breaking change.

I'm making an assumption here, can you confirm that in your test case, that the pause event is also never fired when you go into the "overview" screen? If that's the case, I think the above code is the reasoning why resume event is never fired.

breautek avatar Sep 03 '21 20:09 breautek

Thanks for looking into this. I just now added an alert() at the end of my on_pause(), and when I press the overview button, the alert() shows briefly, so the pause event does fire and on_pause() completes. I tested this on my physical device only (emulator currently not working :/ ).

I've worked around this by calling my on_resume() from the ondeviceready handler, and using a global "onresume_ran" boolean to ensure on_resume() doesn't run twice; now I simply log out if the user takes longer than 10 minutes to return. Not perfect, but it'll do.

Still happy to help troubleshoot this if you want me to try anything else. Cheers.

jmarshall-com avatar Sep 05 '21 01:09 jmarshall-com