PrintDocumentAdapter

Base class that provides the content of a document to be printed.

Lifecycle

The onStart() callback is always the first call you will receive and is useful for doing one time setup or resource allocation before printing. You will not receive a subsequent call here.

The onLayout(PrintAttributes, PrintAttributes, CancellationSignal, LayoutResultCallback, Bundle) callback requires that you layout the content based on the current PrintAttributes . The execution of this method is not considered completed until you invoke one of the methods on the passed in callback instance. Hence, you will not receive a subsequent call to any other method of this class until the execution of this method is complete by invoking one of the callback methods.

The onWrite(PageRange[], ParcelFileDescriptor, CancellationSignal, WriteResultCallback) requires that you render and write the content of some pages to the provided destination. The execution of this method is not considered complete until you invoke one of the methods on the passed in callback instance. Hence, you will not receive a subsequent call to any other method of this class until the execution of this method is complete by invoking one of the callback methods. You will never receive a sequence of one or more calls to this method without a previous call to onLayout(PrintAttributes, PrintAttributes, CancellationSignal, LayoutResultCallback, Bundle) .

The onFinish() callback is always the last call you will receive and is useful for doing one time cleanup or resource deallocation after printing. You will not receive a subsequent call here.

Implementation

The APIs defined in this class are designed to enable doing part or all of the work on an arbitrary thread. For example, if the printed content does not depend on the UI state, i.e. on what is shown on the screen, then you can offload the entire work on a dedicated thread, thus making your application interactive while the print work is being performed. Note that while your activity is covered by the system print UI and a user cannot interact with it, doing the printing work on the main application thread may affect the performance of your other application components as they are also executed on that thread.

You can also do work on different threads, for example if you print UI content, you can handle onStart() and onLayout(PrintAttributes, PrintAttributes, CancellationSignal, LayoutResultCallback, Bundle) on the UI thread (assuming onStart initializes resources needed for layout). This will ensure that the UI does not change while you are laying out the printed content. Then you can handle onWrite(PageRange[], ParcelFileDescriptor, CancellationSignal, WriteResultCallback) and onFinish() on another thread. This will ensure that the main thread is busy for a minimal amount of time. Also this assumes that you will generate the printed content in onLayout(PrintAttributes, PrintAttributes, CancellationSignal, LayoutResultCallback, Bundle) which is not mandatory. If you use multiple threads, you are responsible for proper synchronization.

Summary

Nested Classes
PrintDocumentAdapter.LayoutResultCallback Base class for implementing a callback for the result of onLayout(PrintAttributes, PrintAttributes, CancellationSignal, LayoutResultCallback, Bundle) .
PrintDocumentAdapter.WriteResultCallback Base class for implementing a callback for the result of onWrite(PageRange[], ParcelFileDescriptor, CancellationSignal, WriteResultCallback) .
Constants
String EXTRA_PRINT_PREVIEW Extra: mapped to a boolean value that is true if the current layout is for a print preview, false otherwise.
Public Constructors
Called when printing finishes.

Called when the print attributes (page size, density, etc) changed giving you a chance to layout the content such that it matches the new constraints.

Called when printing starts.

Called when specific pages of the content should be written in the form of a PDF file to the given file descriptor.