Skip to content

Save state on Android#1644

Closed
mdjurfeldt wants to merge 6 commits intoc3d:devfrom
mdjurfeldt:fix-save-state-android
Closed

Save state on Android#1644
mdjurfeldt wants to merge 6 commits intoc3d:devfrom
mdjurfeldt:fix-save-state-android

Conversation

@mdjurfeldt
Copy link

This PR fixes multiple problems regarding saving state on Android devices.

It enables the SaveState function to work properly, after which state will be saved and reloaded when the app launches again.

However, due to permission rules on Android, the autosave of state upon app suspension will not go to the latest user selected file in external storage but an internal file. All saves to external storage needs to be done through SaveState.

@c3d c3d added enhancement New feature or request user Reported by an actual user, yay! labels Mar 4, 2026
Copy link
Owner

@c3d c3d left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot @mdjurfeldt, this looks good overall, but I think you need to clear is_dialog_open on completion of the dialog.

{
#ifdef ANDROID
// Engage the lock. If it was already true (e.g. touch bounce), safely abort.
if (is_dialog_open.exchange(true)) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is is_dialog_open ever set to false?

Copy link
Author

@mdjurfeldt mdjurfeldt Mar 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@c3d Dear Christophe, you are absolutely right. I have now added the set to false and force-pushed a new version.

BTW, many thanks for this amazing project! :-)

Under Android, trying to display a load state message during boot will
often fail due to a race condition with the UI thread. This causes the
message to remain on the screen. Something similar happens when the
app is suspended.

In the simulator on a PC, a load state message during boot is not
needed.

This change eliminates the load and state messages during the above
conditions but does show them otherwise.
On modern Android, android.permission.WRITE_EXTERNAL_STORAGE (as
requested in AndroidManifest.xml) is deprecated. This change uses the
file selector to obtain temporary right to read and write state
files. These are then copied to or from internal storage in order for
further internals to be able to read and write state to said internal
storage.

Note that the read or write to the users selected location will only
be made when using the file selector. Further reads or writes will go
from or to internal storage.

It *is* possible to obtain read and write access to external storage,
but this is discouraged by Google and might cause problems if trying
to get the app available in the Play Store.
The file selector window will generate an ApplicationHidden
event. This change prevents a recursive entry into the save state code
path due to this.
This change enables the Android file selector window to be
shown. Previously, it was not shown due to non-valid parent context
and Linux initialization file path.
@mdjurfeldt mdjurfeldt force-pushed the fix-save-state-android branch from d2d8327 to c658cc5 Compare March 4, 2026 21:10
@mdjurfeldt mdjurfeldt force-pushed the fix-save-state-android branch from c658cc5 to b72c3ca Compare March 4, 2026 22:16
@c3d c3d force-pushed the dev branch 2 times, most recently from 8789ffe to aa8cb69 Compare March 5, 2026 07:14
@mdjurfeldt mdjurfeldt requested a review from c3d March 5, 2026 11:26
@c3d c3d added the done Issues that are done on dev (will be in next release) label Mar 8, 2026
@c3d
Copy link
Owner

c3d commented Mar 8, 2026

Merged on dev, thanks @mdjurfeldt .

@c3d c3d closed this Mar 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

done Issues that are done on dev (will be in next release) enhancement New feature or request user Reported by an actual user, yay!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants