Skip to content

on_exit is not called when closing the app on Android when using eframe #5492

Open
@Pandicon

Description

@Pandicon

Describe the bug

On desktop, the on_exit function is called without issues when closing the app. But on Android, it is not called. This is an issue when using persistence, since the save function is also not called (this seems to be related as save should be called just before on_exit according to the docs) and therefore data is lost if for example some settings are changed and then the user closes the app before the next timed save function call.

To Reproduce
Steps to reproduce the behavior:

  1. Clone the https://github.com/Pandicon/egui-android-example/tree/%60on_exit%60-not-called repository where I set up a basic Android app which showcases this issue
  2. Run the app (initialise the cargo mobile project with cargo mobile init, connect an Android device, allow USB debugging, and do cargo mobile run)
  3. Run adb logcat to see the logs, you can either somehow filter for a specific app or just redirect it into a text file and search for the relevant logs
  4. Let it run for a while - you will see saving! being logged every 30 seconds (at least for me).
  5. Wait to be like 10 seconds past the last saving! log and close the app. Look for a new saving! log (this is why one should wait - to be certain if a given log is the timed one) and an exiting! log. Neither of them exist, so the app was closed without save or on_exit being called.

Expected behavior
on_exit and save are called fine when closing the app (ideally save would also be called when putting the app into background, i.e. "leaving it" as it may get killed by the OS later, but I am not sure if that would not emit the on_exit-triggering event - this would require some testing)

Smartphone (please complete the following information):

  • Device: Galaxy Tab S8
  • OS: Android 14, One UI 6.1.1

Additional context

When using winit, I was able to listen for an event that indicated that the app was being closed/put into the background, where I could call the save function manually. So I feel like it is not a general issue with Android emitting events weirdly or something and that if winit is able to handle it, then eframe should also be.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions