fix: handle import dialog events via ViewModel lifecycle
Purpose / Description
Fix a crash where import dialog handling could run after activity changes and assume a DeckPicker-like context. The issue is an async UI event path that may outlive the originating screen, causing invalid activity assumptions and ClassCastException in rare lifecycle transitions.
Fixes
- Related to #5075
Approach
Introduced an import event pipeline through a dedicated ImportViewModel. ImportDialog now emits import actions as ViewModel events instead of relying only on direct activity casting. AnkiActivity observes those events with lifecycle awareness, so dialog-triggered import actions are consumed only when the UI is in a safe state. Kept backward-compatible behavior: if the current activity already implements ImportDialogListener, handling still works directly. Net effect: removes brittle activity assumptions and aligns async dialog consumption with lifecycle-safe delivery.
How Has This Been Tested?
Build and run AnkiDroid in debug mode. Start app flow that can trigger import confirmation dialogs. Trigger lifecycle transitions around dialog timing
Test configuration used: Android debug build Emulator/device setup used for local development Pre-commit hooks passed during commit, including ktlint
Checklist
Please, go through these checks before submitting the PR.
- [x] You have a descriptive commit message with a short title (first line, max 50 chars).
- [x] You have commented your code, particularly in hard-to-understand areas
- [x] You have performed a self-review of your own code
- [ ] UI changes: include screenshots of all affected screens (in particular showing any new or changed strings)
- [ ] UI Changes: You have tested your change using the Google Accessibility Scanner
@mixelas Are you able to reproduce this issue ? If yes, May I know the exact reproduction steps, coz I can't reproduce
@sanjaysargam I wasn’t able to reproduce it reliably either. The issue appears to be timing/lifecycle dependent, so there isn’t a deterministic set of steps that reproduces it every time. The closest scenario I could infer from the crash reports and code path is: Open AnkiDroid through a non-DeckPicker path, such as Reviewer first or via a file manager. Trigger an import flow while the current activity is transitioning or no longer the one that originally started the dialog. The import confirmation dialog is then handled by an activity that does not implement the expected listener, which leads to the crash. The fix is meant to make that path safe even when the exact reproduction is hard to hit.
I'd propose closing this until reproduction steps and a regression test exists, it should be a last-ditch effort to introduce a fix for a bugs which we can't reproduce.
@lukstbit assigned this to me and explicitly approved this ViewModel approach as the right solution. I followed his guidance and implemented it. This issue has real ACRA crash reports in production and the fix uses a standard pattern already in AnkiDroid. I can add unit/integration tests to strengthen confidence. The timing-dependent nature makes 100% repro hard, but the fix is architecturally sound.
I'll wait for a reply, but it's unexpected for someone to produce a fix before reproducing an issue.
Our contributor guide rules aren't "hard" rules, but I feel skipping reproduction steps often requires a strong justification. I've had cases where I was able to remove hundreds of lines of code once a problem was properly understood
I'd propose closing this until reproduction steps and a regression test exists, it should be a last-ditch effort to introduce a fix for a bugs which we can't reproduce.
While we can't reproduce this exactly it's clear IMO what's happening. Sending messages to the main Looper to show dialogs was primitive 5 years ago and even worse now. Any improvements(and I do consider these improvements) is a step forward.
Plus this back and forth with destinations through a ViewModel is not something new and we use it.
Given
- https://github.com/ankidroid/Anki-Android/pull/21018/
Let's go back to the ViewModel approach. Sorry for the mixed messages
I switched it to the ViewModel-based approach from #21018, so the import dialog is now handled the same way as that async dialog fix.
Thanks a lot for the detailed review and patience here. I really appreciate it. I’m very open to further changes. I’m trying to learn the preferred project patterns as I go.
Snapshot diff report vs main. Open screenshot-diff for diffs.
- PreferencesScreenshotTest: 1 change
All 1 changed screenshots
PreferencesScreenshotTest
AboutFragment_compare.png
please rebase onto main rather than including merge commits
I apologize for the confusion, my intent was to revert the commit, rebase my changes to keep the history clean, and force-push the finalized updates.
@david-allison Can you please review again.
Thanks!! There should be a newline between the commit message title and body (and ideally squash the commits when you're there)
fix: use ViewModel to handle import dialogs safely
Assisted-by: Claude Opus 4.8 - Flow updates
Co-authored-by: David Allison <[email protected]>