rust-mobile/android-activity

Add an `AndroidApp::set_save_state()` API and correctly restore state for multiple `Resume`s during process lifetime

rib opened this issue · 0 comments

rib commented

Currently state saving is based on the android_native_app_glue design whereby there is a saved_state, malloc() allocation and saved_state_size that are only allowed to be modified during MainEvent::SaveState.

This state is initialized during onCreate but it is cleared after each Resume event.

This means that if there are multiple Resume events (can happen when switching Android applications) then later Resume events may be passed no state (and even the first Resume may get no state if no state was passed to onCreate.

The current design is also awkward from the POV that we are tracking a malloc() allocation whose ownership is context sensitive. It is owned by the app when passed to onCreate and it is owned by the ANativeActivity when it is set during MainEvent::SaveState.

Instead of tracking this malloc() allocation state we should simply track a save_state: Vec<u8> that can be updated and queried at any time. When ANativeActivity needs to get this state via onSaveInstanceState then we can simply copy the state into a transient malloc() allocation.