feat: handle app lifecycle events (Pause/Resume/Background/Foreground)#736
Open
TigerInYourDream wants to merge 2 commits intoproject-robius:mainfrom
Open
feat: handle app lifecycle events (Pause/Resume/Background/Foreground)#736TigerInYourDream wants to merge 2 commits intoproject-robius:mainfrom
TigerInYourDream wants to merge 2 commits intoproject-robius:mainfrom
Conversation
Save and restore app state across lifecycle transitions to support mobile platforms (especially Android) where the OS may suspend or background the app at any time. Key changes: - Replace monolithic Shutdown handler with a match on lifecycle events - On Pause/Background: save window geometry + AppState, stop sync service - On Foreground/Resume: restore state, restart sync, trigger full redraw - On Shutdown: save everything including TSP wallet (destructive, exit-only) - Add `state_saved` flag to deduplicate saves and gate restores - Factor save/restore logic into three methods on App: `save_lifecycle_state`, `save_all_state`, `restore_all_state` Design notes: - TSP's close_and_serialize() consumes ownership and tsp_init() uses OnceLock, so TSP state can only be saved on Shutdown (not Pause) - SyncService stop()/start() are idempotent — safe to call repeatedly - All blocking async calls use timeouts (2-3s) to avoid ANR on Android - Preserves logged_in state on restore to avoid stale persisted values Closes project-robius#458
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Implements app lifecycle event handling as described in #458. The app now properly saves/restores state and stops/starts the Matrix sync service when the OS suspends, backgrounds, or resumes the application.
This is especially important on Android, where the OS may pause or kill the app at any time. Previously, only
Shutdownwas handled — meaning state could be lost if the process was killed without a clean shutdown.Changes
Single file modified:
src/app.rs(+118 / -27 lines)New lifecycle event handling
Replaced the monolithic
if let Event::Shutdownblock with amatchon lifecycle events:Pause/BackgroundForeground/ResumeShutdownNew methods on
Appsave_lifecycle_state— Saves window geometry andAppStateto persistent storage. Best-effort: continues saving remaining parts even if one fails.save_all_state— Callssave_lifecycle_state+ saves TSP wallet state. TSP save is destructive (close_and_serializeconsumesTspState), so this is only called onShutdown.restore_all_state— Restores window geometry andAppState. Preserves the currentlogged_inflag to avoid overwriting it with a stale persisted value.Deduplication via
state_savedflagA
state_saved: boolfield onAppprevents redundant saves during the outgoing lifecycle sequence (Pause → Background → Shutdown) and gates restores so they only run after a save actually occurred.Design Decisions
TSP only on Shutdown:
close_and_serialize()consumes ownership andtsp_init()usesOnceLock(one-time init), so TSP state cannot be round-tripped across Pause/Resume cycles.Idempotent sync service calls:
SyncService::stop()andstart()are idempotent, so calling them on both Pause+Background (or Resume+Foreground) is safe.Timeouts on all blocking async: All
block_on_async_with_timeoutcalls use 2–3 second timeouts to stay well under Android's 5-second ANR limit.No platform-specific code: All lifecycle events are platform-agnostic Makepad
Eventvariants — no#[cfg(target_os)]needed.Preserve
logged_inon restore: The persistedAppStatemay contain a stalelogged_invalue, so we explicitly preserve the current runtime value after restoring.Closes #458