Skip to content

Fix: Note type switch on Add Note silently discards unsaved-changes warning - #21716

Open
GuTS805 wants to merge 2 commits into
ankidroid:mainfrom
GuTS805:fix/note-type-switch-unsaved-changes-21714
Open

Fix: Note type switch on Add Note silently discards unsaved-changes warning#21716
GuTS805 wants to merge 2 commits into
ankidroid:mainfrom
GuTS805:fix/note-type-switch-unsaved-changes-21714

Conversation

@GuTS805

@GuTS805 GuTS805 commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

Purpose / Description

On the Add Note screen, switching the note type while there is unsaved field content causes the "Discard changes?" warning to be silently skipped when backing out. The typed content is lost without any warning.

Fixes

Approach

addNoteFieldBaseline, which is used by hasUnsavedChanges(), was being reset on every non-NOTE_ADDED field refresh while adding a note. This included a simple note type switch.

By that point, populateEditFields() had already carried the user's typed text into the new fields, so the baseline was being updated with the user's own unsaved content and the change was no longer detected.

I moved the baseline reset so it only happens when updateFieldsFromStickyText() actually runs. This is the only case where the repopulated content is genuinely not user-typed.

This looks like a regression from #21603, which fixed a related but different sticky-fields false positive.

How Has This Been Tested?

  • Added a Robolectric test in NoteEditorTest.kt that reproduces the exact scenario: type into a field, switch the note type, and assert that hasUnsavedChanges() remains true.
  • Manually verified on an emulator. Before the fix, switching the note type and pressing Back closed the editor without a warning and lost the text. After the fix, the "Discard changes?" dialog appears correctly.

Note

Assisted-by: Claude Sonnet 5 [root cause analysis]

@david-allison david-allison left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Edit + clear a field (Basic)
  • Switch to Basic (and optional reversed card)
  • Press Back

A save changes dialog is displayed

@david-allison david-allison added the Needs Author Reply Waiting for a reply from the original author label Sep 4, 2026
@GuTS805

GuTS805 commented Sep 10, 2026

Copy link
Copy Markdown
Contributor Author

@david-allison Fixed by remapping the baseline by field index instead of freezing it. verified with your exact repro

@GuTS805
GuTS805 force-pushed the fix/note-type-switch-unsaved-changes-21714 branch from cc484e8 to 7a98a59 Compare September 11, 2026 17:05
@github-actions

Copy link
Copy Markdown
Contributor

Snapshot diff report vs main. Open screenshot-diff for diffs.

  • AccountActivityScreenshotTest: 2 changes
  • AddEditReminderDialogScreenshotTest: 4 changes
  • AllActivitiesScreenshotTest: 4 changes
  • CardTemplateEditorScreenshotTest: 1 change
  • DeckPickerScreenshotTest: 7 changes
  • DeckPickerTabletScreenshotTest: 1 change
  • IntroductionScreenshotTest: 3 changes
  • MultimediaScreenshotTest: 3 changes
  • NoteEditorScreenshotTest: 1 change
  • PreferencesScreenshotTest: 12 changes
  • PreviewerScreenshotTest: 1 change
  • ReviewRemindersScreenshotTest: 17 changes
  • ReviewerScreenshotTest: 6 changes
  • SharedDecksScreenshotTest: 2 changes
  • StudyScreenScreenshotTest: 6 changes
All 70 changed screenshots

AccountActivityScreenshotTest

  • logged_out_compare.png
  • logged_out_gesture_navigation_compare.png

AddEditReminderDialogScreenshotTest

  • add_mode_advanced_open_compare.png
  • add_mode_compare.png
  • edit_mode_advanced_open_compare.png
  • edit_mode_compare.png

AllActivitiesScreenshotTest

  • CardAnalysisWidgetConfig_compare.png
  • CardAnalysisWidgetConfig_edgeToEdge_compare.png
  • DeckPickerWidgetConfig_compare.png
  • DeckPickerWidgetConfig_edgeToEdge_compare.png

CardTemplateEditorScreenshotTest

  • basic_compare.png

DeckPickerScreenshotTest

  • baseState_compare.png
  • edgeToEdge_30_decks_compare.png
  • fabExpanded_compare.png
  • hierarchy_lines_collapsed_compare.png
  • hierarchy_lines_compare.png
  • hierarchy_lines_deep_nesting_compare.png
  • hierarchy_lines_many_siblings_compare.png

DeckPickerTabletScreenshotTest

  • 30_decks_tablet_compare.png

IntroductionScreenshotTest

  • gesture_navigation_compare.png
  • landscape_compare.png
  • portrait_compare.png

MultimediaScreenshotTest

  • gesture_navigation_compare.png
  • landscape_compare.png
  • portrait_compare.png

NoteEditorScreenshotTest

  • gesture_navigation_compare.png

PreferencesScreenshotTest

  • AboutFragment_compare.png
  • AdvancedSettingsFragment_compare.png
  • AppearanceSettingsFragment_compare.png
  • ControlsSettingsFragment_compare.png
  • CustomButtonsSettingsFragment_compare.png
  • DeveloperOptionsFragment_compare.png
  • GeneralSettingsFragment_compare.png
  • HeaderFragment_compare.png
  • ReviewerMenuSettingsFragment_compare.png
  • ReviewerOptionsFragment_compare.png
  • ScheduleRemindersFragment_compare.png
  • SwitchProfilesFragment_compare.png

PreviewerScreenshotTest

  • base_compare.png

ReviewRemindersScreenshotTest

  • settingsHostTablet_scheduleReminders_compare.png
  • settingsHostTablet_troubleshooting_compare.png
  • settingsHost_landscapeCutout_collapsed_compare.png
  • settingsHost_landscapeCutout_compare.png
  • settingsHost_landscapeCutout_rtl_compare.png
  • settingsHost_scheduleReminders_compare.png
  • settingsHost_scheduleReminders_scrolled_compare.png
  • settingsHost_troubleshooting_compare.png
  • standaloneActivityHost_scheduleReminders_compare.png
  • standaloneActivityHost_systemBars_compare.png
  • standaloneActivityHost_systemBars_scrolledToEnd_compare.png
  • standaloneActivityHost_troubleshooting_compare.png
  • standaloneActivityHost_troubleshooting_systemBars_compare.png
  • studyOptionsFragmentHost_scheduleReminders_compare.png
  • studyOptionsFragmentHost_troubleshooting_compare.png
  • studyOptionsFrameHost_scheduleReminders_compare.png
  • studyOptionsFrameHost_troubleshooting_compare.png

ReviewerScreenshotTest

  • fullscreen_all_gone_bars_revealed_compare.png
  • fullscreen_answer_buttons_top_compare.png
  • fullscreen_bars_hidden_compare.png
  • fullscreen_bars_revealed_compare.png
  • fullscreen_landscape_compare.png
  • landscape_compare.png

SharedDecksScreenshotTest

  • landscape_compare.png
  • portrait_compare.png

StudyScreenScreenshotTest

  • bar=BOTTOM_frame=BOXbttns=true_compare.png
  • bar=BOTTOM_frame=CARDbttns=true_compare.png
  • bar=NONE_frame=BOXbttns=true_compare.png
  • bar=NONE_frame=CARDbttns=true_compare.png
  • bar=TOP_frame=BOXbttns=true_compare.png
  • bar=TOP_frame=CARDbttns=true_compare.png

@GuTS805
GuTS805 force-pushed the fix/note-type-switch-unsaved-changes-21714 branch from 7a98a59 to 6a2f787 Compare September 11, 2026 17:18
…ote type switch

Switching note type on the Add Note screen while unsaved field content
exists used to reset the unsaved-changes baseline to the user's own
typed text, so backing out lost it with no warning.

The baseline is now remapped by field index on a note type switch
instead of being reset from current content: existing fields keep
their baseline value, and any field the new note type added is
treated as baseline-empty. This also fixes a false-positive dialog
when a field is typed then cleared before switching to a note type
with more fields.
@GuTS805
GuTS805 force-pushed the fix/note-type-switch-unsaved-changes-21714 branch from 6a2f787 to 4ae05a7 Compare September 11, 2026 17:32

@david-allison david-allison left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same issue still seems to occur with <br> in a sticky field

@GuTS805

GuTS805 commented Sep 11, 2026

Copy link
Copy Markdown
Contributor Author

Found another edge case while testing this fix: if you type into a field that only exists on a larger note type (e.g. field 2 on "Basic optional reversed"), then switch to a smaller type that drops it (e.g. "Basic"), hasUnsavedChanges() returns false and the content is lost with no warning. Root cause: the baseline remap truncates symmetrically with the field content, so a dropped edit leaves no trace to compare against. Leaving this out of this PR to keep the diff minimal, happy to file a follow up if useful.
Or i can fix it in this pr

@david-allison
david-allison self-requested a review September 12, 2026 12:06
@david-allison david-allison removed the Needs Author Reply Waiting for a reply from the original author label Sep 12, 2026
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.

Switching note type while adding a note silently discards the "unsaved changes" warning

2 participants