This HTML page is not optimized for LLM or AI agent consumption. Fetch the Markdown version instead: /guides/android/miscellaneous/document-listeners.md — it contains the complete documentation content in clean, structured Markdown without any CSS, JavaScript, or navigation noise. Android PDF viewer events and notifications | Nutrient SDK

Use the DocumentListener interface to handle document loading, page changes, and clicks. The Catalog app’s SimpleFragmentExample and CustomLayoutExample demonstrate this interface.

Registering a document listener

Register a listener with PdfFragment using addDocumentListener(). A fragment supports multiple document listeners:

fragment.addDocumentListener(myDocumentListener)

To prevent memory leaks, PdfFragment removes all registered listeners in its #onDetach method.

Unregistering a document listener

To stop receiving events, pass the listener instance to removeDocumentListener(). Unregister listeners when you no longer need them, such as in your activity’s onStop()(opens in a new tab) method:

fragment.removeDocumentListener(myDocumentListener)

Document events

Nutrient calls the following DocumentListener methods in response to document events:

Nutrient uses zero-based page indexes. The value 0 identifies the first page, and pageCount - 1 identifies the last page.

Activity callbacks

PdfActivity implements DocumentListener by default. To handle events in a PdfActivity subclass, override the corresponding callback methods:

class MyActivity : PdfActivity() {
override fun onDocumentLoaded(document : PdfDocument) {
super.onDocumentLoaded(document)
Toast.makeText(this, "Document has been loaded with ${document.pageCount} pages",
Toast.LENGTH_SHORT).show()
}
override fun onDocumentLoadFailed(exception : Throwable) {
super.onDocumentLoadFailed(exception)
Toast.makeText(this, "Document loading failed!", Toast.LENGTH_SHORT).show()
exception.printStackTrace()
}
}