---
title: "Detect if PDF annotation has changed on Android | Nutrient"
canonical_url: "https://www.nutrient.io/guides/android/annotations/create-edit-and-remove/detect-changes/"
md_url: "https://www.nutrient.io/guides/android/annotations/create-edit-and-remove/detect-changes.md"
last_updated: "2026-05-30T02:20:01.149Z"
description: "After loading a document, you can access its annotations via the AnnotationProvider returned by PdfDocument#getAnnotationProvider."
---

# Detect if an annotation has changed on Android

After loading a document, you can access its annotations via the [`AnnotationProvider`](https://www.nutrient.io/api/android/nutrient/com.pspdfkit.annotations/-annotation-provider/index.html) returned by [`PdfDocument#getAnnotationProvider`](https://www.nutrient.io/api/android/nutrient/com.pspdfkit.document/-pdf-document/get-annotation-provider.html). The annotation provider supports reading, adding, and removing annotations to and from the document associated with it:

### KOTLIN

```kotlin

val pageIndex = 0
val annotations: List<Annotation> = document.annotationsProvider.getAnnotations(pageIndex)

```

### JAVA

```java

final int pageIndex = 0;
final List<Annotation> annotations = document.getAnnotationsProvider().getAnnotations(pageIndex);

```

## Listening for annotation changes

The [`PdfFragment`](https://www.nutrient.io/api/android/nutrient/com.pspdfkit.ui/-pdf-fragment/index.html) implements the [`OnAnnotationSelectedListener`][] interface, allowing you to register listeners that are notified whenever an annotation is selected, modified, or deselected:

```kt

override fun onCreate(savedInstanceState : Bundle?) {
    super.onCreate(savedInstanceState)

    pdfFragment.addOnAnnotationSelectedListener(object :OnAnnotationSelectedListener {
        override fun onPrepareAnnotationSelection(controller: AnnotationSelectionController, annotation: Annotation, annotationCreated: Boolean): Boolean {
            // Returning `false` here would prevent the annotation from being selected.
            return true
        }

        override fun onAnnotationSelected(annotation: Annotation, annotationCreated: Boolean) {
            Log.i(TAG, "The annotation was selected.")
        }

        override fun onAnnotationDeselected(annotation: Annotation, reselected: Boolean) {
            Log.i(TAG, "The annotation was deselected.")
        }
    })

    pdfFragment.addOnAnnotationUpdatedListener(object: OnAnnotationUpdatedListener {
        override fun onAnnotationCreated(annotation: Annotation) {
            Log.i(TAG, "The annotation was created.")
        }

        override fun onAnnotationUpdated(annotation: Annotation) {
            Log.i(TAG, "The annotation was updated.")
        }

        override fun onAnnotationRemoved(annotation: Annotation) {
            Log.i(TAG, "The annotation was removed.")
        }
    })
}

```

### Modifying and saving annotations

If an annotation is modified (i.e. if it has been changed since the document has been loaded) a call to [`Annotation#isModified`](https://www.nutrient.io/api/android/nutrient/com.pspdfkit.annotations/-annotation/is-modified.html) will return `true`. Furthermore, the [`PdfDocument#wasModified`](https://www.nutrient.io/api/android/nutrient/com.pspdfkit.document/-pdf-document/was-modified.html) method will return `true` if annotations were added, changed, or removed. Once you save the document and its annotations, they’re no longer marked as modified.

If you’re editing annotations using one of the annotation tools, modifications to the edited annotation and document will only be visible after you exit the current tool mode by calling [`PdfFragment#exitCurrentlyActiveMode`]. If the annotation tool is still active (i.e. the tool is selected in the annotation creation toolbar), [`PdfDocument#wasModified`](/api/android/nutrient/com.pspdfkit.document/-pdf-document/was-modified.html#wasModified\(\)) will still return `false`.

To save a document and its annotations, you can use any of the synchronous or asynchronous save methods on the [`PdfDocument`](https://www.nutrient.io/api/android/nutrient/com.pspdfkit.document/-pdf-document/index.html)&nbsp;class. The following example uses [`PdfDocument#saveIfModified`](https://www.nutrient.io/api/android/nutrient/com.pspdfkit.document/-pdf-document/save-if-modified.html), which writes the document back to its original location after testing if it has been modified:

### KOTLIN

```kotlin

override fun onDocumentLoaded(document : PdfDocument) {
    assert(document.wasModified() == false)

    // Add an annotation to the document.
    val annotation = NoteAnnotation(0, RectF(100, 132, 132, 100), "Test annotation", NoteAnnotation.CROSS)
    document.annotationProvider.addAnnotationToPage(annotation)

    assert(annotation.isModified() == true)
    assert(document.wasModified() == true)

    // This will write the document back to its original location.
    document.saveIfModified()

    assert(annotation.isModified() == false)
    assert(document.wasModified() == false)
}

```

### JAVA

```java

@Override public void onDocumentLoaded(@NonNull PdfDocument document) {
    assert document.wasModified() == false;

    // Add an annotation to the document.
    NoteAnnotation annotation = new NoteAnnotation(0, new RectF(100, 132, 132, 100), "Test annotation", NoteAnnotation.CROSS);
    document.getAnnotationProvider().addAnnotationToPage(annotation);

    assert annotation.isModified() == true;
    assert document.wasModified() == true;

    // This will write the document back to its original location.
    document.saveIfModified();

    assert annotation.isModified() == false;
    assert document.wasModified() == false;
}

```
---

## Related pages

- [Set annotation author names on Android easily](/guides/android/annotations/annotation-author-name.md)
- [Disabling annotation editing on Android](/guides/android/annotations/configuring-annotation-editing.md)
- [Programmatically create annotations on Android](/guides/android/annotations/programmatically-creating-annotations.md)
- [Define annotation behavior with flags on Android](/guides/android/annotations/annotation-flags.md)
- [Z-Index for annotation stacking order on Android](/guides/android/annotations/annotation-z-index.md)
- [Copy and paste annotations on Android](/guides/android/features/copy-paste.md)
- [Embed or attach a file to a PDF on Android](/guides/android/annotations/file-annotations.md)
- [Undo and redo annotations on Android](/guides/android/features/undo-redo.md)

