Skip to content

fix: field deletion confirmation after recreation - #22767

Closed
Ranjan-3105 wants to merge 1 commit into
ankidroid:mainfrom
Ranjan-3105:fix/confirmation-dialog-callback-recreation
Closed

Ranjan-3105 wants to merge 1 commit into
ankidroid:mainfrom
Ranjan-3105:fix/confirmation-dialog-callback-recreation

Conversation

@Ranjan-3105

Copy link
Copy Markdown
Contributor

Purpose / Description

Deleting a field in the note-type field editor could silently fail if the activity was recreated while the confirmation dialog was open.

ConfirmationDialog stored its confirmation action as an in-memory Runnable. When the activity was recreated, for example, after switching to night mode, the dialog was restored but the Runnable was not. Consequently, confirming the deletion could do nothing, leaving the field options menu visible without deleting the field.

Fixes

Approach

  • Added optional Fragment Result API support to ConfirmationDialog. The request key is stored in the dialog's arguments, allowing the confirmation result to be delivered after activity recreation.
  • Preserved the existing setConfirm() and setCancel() behavior for other callers.
  • Updated NoteTypeFieldEditor to handle the deletion confirmation through a Fragment Result listener registered in onCreate.
  • Applied the new confirmation mechanism to both the normal deletion warning and the full-sync confirmation path.
  • Preserved the selected field position across activity recreation.

How Has This Been Tested?

Unit tests (JVM / Robolectric):

  • Added NoteTypeFieldEditorRecreationTest, which opens the deletion confirmation, recreates the activity, confirms the deletion, and verifies that the intended field is deleted.
  • Existing NoteTypeFieldEditorTest: 10 tests passed.
  • Full :AnkiDroid:testPlayDebugUnitTest task completed successfully.
  • ktlint and pre-commit hooks completed successfully.

Commands used:

.\gradlew.bat :AnkiDroid:testPlayDebugUnitTest --tests "com.ichi2.anki.NoteTypeFieldEditorRecreationTest" --tests "com.ichi2.anki.NoteTypeFieldEditorTest"

.\gradlew.bat :AnkiDroid:testPlayDebugUnitTest

Learning (optional, can help others)

In-memory callbacks belong to a fragment instance and are not automatically restored when that fragment is recreated. Fragment arguments and saved state, however, can survive recreation.

Using fragment arguments together with the Fragment Result API allows the dialog to return a confirmation result to a listener registered by the recreated activity.

See the Android Fragment communication guide.

Checklist

  • You have a descriptive commit message with a short title (first line, max 50 chars).
  • You have commented your code, particularly in hard-to-understand areas
  • 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

@david-allison

Copy link
Copy Markdown
Member

Please read our AI policy: https://github.com/ankidroid/Anki-Android/blob/main/AI_POLICY.md

@Ranjan-3105

Copy link
Copy Markdown
Contributor Author

I am sorry, I should have read the AI policy beforehand. Also in the PR description I atleast should have disclosed. I was frustrated with the errors I was going through so decided to use AI.

I understand the workflow but am new to android development. I have now read the AI policy and will abide by it from this point on, please direct me with the appropriate next step I should take.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ConfirmationDialog loses callbacks on activity recreation

2 participants