apache / apache/cordova-android

After upgrading everything, in low memory, localStorage in onpause/onresume doesn't seem to work

Open
#1,340 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
3.8k
Forks
1.6k
Avg merge
16h 18m
Merged PRs (30d)
11

Description

# Bug Report

## Problem

In low memory, and on physical device only: In my `pause` event handler, I do `localStorage.setItem(...)` . Then in my `resume` and `deviceready` event handlers, I do `localStorage.getItem(...)`, but the retrieved value is always null. When using the emulator, it works as expected.

### What is expected to happen?

`localStorage.getItem()` in `resume` or `deviceready` should get back the value that was set in `localStorage.setItem(...)` .

### What does actually happen?

`localStorage.getItem()` in those handlers just returns `null`.

## Information

This only happens on a physical device (I'm using a Pixel 2 XL). All is OK in the emulator.

An `alert()` in the `pause` handler (briefly) shows that the value is correctly set with `localStorage.setItem()` .

This could be related to bug #1319 .

### Command or Code
HTML file `index.html` is:
```







```
JavaScript file `index2.js` is:
```
"use strict" ;

var app = {
initialize: function() {
document.addEventListener('deviceready', this.onDeviceReady.bind(this), false);
},

onDeviceReady: async function() {
alert('ondeviceready') ;
document.addEventListener('pause', on_pause, false) ;
document.addEventListener('resume', on_resume, false) ;

let saved_state= localStorage.getItem('saved_state') ;
alert('in ondeviceready, saved_state='+saved_state) ; // is always null here when on physical device
localStorage.removeItem('saved_state') ;
},
};

app.initialize();

function on_pause() {
localStorage.setItem('saved_state', 'test') ;
alert('end of onpause, saved_state=['+localStorage.getItem('saved_state')+']') ;
}

function on_resume(event) {
// this never runs-- apparently resume event isn't fired
alert('in onresume, saved_state=['+localStorage.getItem('saved_state')+']') ;
}
```

### Environment, Platform, Device

Using Linux for my dev machine, and testing on a physical Pixel 2 XL running Android 11.

### Version information
Cordova CLI 10.0.0
Pixel 2 XL running Android 11

output of `cordova plugin ls`:
```
cordova-clipboard 1.3.0 "Clipboard"
cordova-plugin-add-swift-support 2.0.2 "AddSwiftSupport"
cordova-plugin-android-permissions 1.1.2 "Permissions"
cordova-plugin-androidx-adapter 1.1.3 "cordova-plugin-androidx-adapter"
cordova-plugin-autostart 2.3.0 "Autostart"
cordova-plugin-background-mode 0.7.3 "BackgroundMode"
cordova-plugin-camera 6.0.0 "Camera"
cordova-plugin-device 2.0.3 "Device"
cordova-plugin-fetch 0.1.0 "Cordova Fetch"
cordova-plugin-file 6.0.2 "File"
cordova-plugin-photo-library 2.2.1 "Photo Library"
cordova-plugin-save-to-camera-roll 1.0.2 "Save to camera roll"
cordova-sqlcipher-adapter 0.4.0 "Cordova sqlcipher adapter"
cordova-sqlite-storage 6.0.0 "Cordova SQLite storage plugin - cordova-sqlite-storage plugin version"
```

output of `cordova platform ls`:
```
6.0.0
Installed platforms:
android 10.1.0
browser
ios 6.1.1
Available platforms:
browser ^6.0.0
electron ^1.0.0
```

## Checklist

- [x] I searched for existing GitHub issues
- [x] I updated all Cordova tooling to most recent version
- [x] I included all the necessary information above

Contributor guide

Open the contributing guide

Research direction

Reproduce the report with index.html and js/index2.js on a physical Pixel 2 XL running Android 11, exercising the pause, resume, and deviceready handlers. Then inspect Cordova Android's lifecycle handling and localStorage behavior around those events. Done means the saved_state value persists and can be retrieved as expected after low-memory recovery on a physical Android device.

Written by the indexing model from the issue text.

Assessment

Tech stack
android, javascript
Domain
mobile
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.