Undo and redo annotations on Android
Nutrient Android SDKdsupports undo and redo for creating and editing annotations. Users can undo and redo changes using the buttons in AnnotationCreationToolbar
and AnnotationEditingToolbar
.
To achieve this, we’ve created an interface named UndoManager
. The implementation of this interface is provided through the PdfFragment#getUndoManager()
method. The UndoManager
provides methods for controlling the undo/redo business logic, including:
-
undo()
— undo the last performed edit -
redo()
— redo the last undone edit -
canUndo()
— whether or not the undo action can be executed -
canRedo()
— whether or not the redo action can be executed
Enabling/disabling undo and redo
Undo and redo operations are enabled by default. However, you can use undoEnabled(boolean)
and redoEnabled(boolean)
when building a PdfConfiguration
to control whether or not undo and redo are enabled or disabled in your app. Logically, redo cannot be enabled if undo is disabled.
Listening for undo and redo changes
To listen for undo and redo changes, you can attach OnUndoHistoryChangeListener
to the UndoManager
through addOnUndoHistoryChangeListener()
. The callback will notify you once the undo history changes so that you can update the UI or components that need to be aware of the undo/redo state.
Intercepting edits
To intercept editing operations to read details or to block adding to the undo stack, implement the OnAddNewEditListener
interface and add it to the UndoManager
to get notified when edits are about to be added.
Here’s an example that blocks annotation creation from being added to the undo stack:
class MyActivity : PdfActivity(), OnAddNewEditListener { override fun onDocumentLoaded(document: PdfDocument) { super.onDocumentLoaded(document) // Hook listener to the SDK. pdfFragment?.undoManager?.setOnAddNewEditListener(this) } override fun onAddNewEdit(edit: Edit): Boolean { // This override blocks an edit that is triggered from adding an annotation. // It allows every other edit operation to be added to the undo stack. return !(edit is AnnotationAddRemoveEdit && edit.type == AnnotationAddRemoveEdit.Type.ADD_ANNOTATION) } }
Current limitations
-
Currently, undo/redo history is tracked for annotations and content editing operations.