EditablePdfViewerFragment


@RequiresExtension(extension = 31, version = 18)
public class EditablePdfViewerFragment extends PdfViewerFragment


A androidx.fragment.app.Fragment that extends PdfViewerFragment to provide PDF editing capabilities, including annotation and form filling, leveraging the 'androidx.ink' library.

This fragment coordinates the underlying PDF content with editing layers, enabling users to add ink strokes, create annotations, and modify form fields. It manages the interaction logic between viewing the document and performing edits.

Editing Workflow:

  1. Viewing: Behaves exactly like [PdfViewerFragment].
  2. Editing: When [isEditModeEnabled] is set to `true`, user can leverage editing capabilities(such as annotating or filling forms).
  3. Saving: Edits are accumulated as "drafts". To persist changes, the host must call [applyDraftEdits], which asynchronously applies unsaved edits and creates a [PdfWriteHandle] used to write the modified document to a file.

Summary

Public constructors

Protected constructors

Public methods

final void

Applies all draft edits to the document.

final boolean

Returns true if there are any draft edits that have not yet been applied to the document, false otherwise.

final boolean

Returns true if an applyDraftEdits operation is currently in progress.

final boolean

If true, the fragment is in edit mode, allowing for annotating or editing.

void

Callback invoked when applying draft edits has failed.

void

Callback invoked when draft edits have been successfully applied to the document.

void

Called when a fragment is first attached to its context.

View
onCreateView(
    @NonNull LayoutInflater inflater,
    ViewGroup container,
    Bundle savedInstanceState
)

Called to have the fragment instantiate its user interface view.

void

Called when the view previously created by onCreateView has been detached from the fragment.

void

Called when the fragment is no longer attached to its activity.

void

Callback invoked when EditablePdfViewerFragment enters edit mode.

void

Callback invoked when EditablePdfViewerFragment exits edit mode.

void
onViewCreated(@NonNull View view, Bundle savedInstanceState)

Called immediately after onCreateView has returned, but before any saved state has been restored in to the view.

final void
setEditModeEnabled(boolean value)

If true, the fragment is in edit mode, allowing for annotating or editing.

Inherited methods

From androidx.activity.result.ActivityResultCaller
final @NonNull ActivityResultLauncher<@NonNull I>
@MainThread
<I extends Object, O extends Object> registerForActivityResult(
    @NonNull ActivityResultContract<@NonNull I, @NonNull O> contract,
    @NonNull ActivityResultCallback<@NonNull O> callback
)

Register a request to start an activity for result, designated by the given contract.

final @NonNull ActivityResultLauncher<@NonNull I>
@MainThread
<I extends Object, O extends Object> registerForActivityResult(
    @NonNull ActivityResultContract<@NonNull I, @NonNull O> contract,
    @NonNull ActivityResultRegistry registry,
    @NonNull ActivityResultCallback<@NonNull O> callback
)

Register a request to start an activity for result, designated by the given contract.

From android.content.ComponentCallbacks
void
void

This method is deprecated.

From androidx.fragment.app.Fragment
void
dump(
    @NonNull String prefix,
    @Nullable FileDescriptor fd,
    @NonNull PrintWriter writer,
    @Nullable String[] args
)

Print the Fragments's state into the given stream.

final @Nullable FragmentActivity
boolean
boolean
final @Nullable Bundle
final @NonNull FragmentManager
@Nullable Context
@NonNull CreationExtras
@NonNull ViewModelProvider.Factory
@Nullable Object
@Nullable Object
final @Nullable FragmentManager

This method is deprecated. Deprecated in Java

final @Nullable Object
final int
final @NonNull LayoutInflater
@NonNull Lifecycle
@NonNull LoaderManager

This method is deprecated. Deprecated in Java

final @Nullable Fragment
final @NonNull FragmentManager
@Nullable Object
final @NonNull Resources
final boolean

This method is deprecated. Deprecated in Java

@Nullable Object
final @NonNull SavedStateRegistry
@Nullable Object
@Nullable Object
final @NonNull String
getString(@StringRes int resId)

Return a localized string from the application's package's default string table.

final @NonNull String
getString(@StringRes int resId, @Nullable Object... formatArgs)

Return a localized formatted string from the application's package's default string table, substituting the format arguments as defined in java.util.Formatter and format.

final @Nullable String
final @Nullable Fragment

This method is deprecated. Deprecated in Java

final int

This method is deprecated. Deprecated in Java

final @NonNull CharSequence
getText(@StringRes int resId)

Return a localized, styled CharSequence from the application's package's default string table.

boolean

This method is deprecated. Deprecated in Java

@Nullable View
@NonNull LifecycleOwner
@NonNull LiveData<@NonNull LifecycleOwner>
@NonNull ViewModelStore
final boolean
final boolean
final boolean
final boolean
final boolean
final boolean
final boolean
final boolean
void

This method is deprecated.

use onViewCreated for code touching the view created by onCreateView and onCreate for other initialization.

void
onActivityResult(int requestCode, int resultCode, @Nullable Intent data)

This method is deprecated.

This method has been deprecated in favor of using the Activity Result API which brings increased type safety via an ActivityResultContract and the prebuilt contracts for common intents available in androidx.activity.result.contract.ActivityResultContracts, provides hooks for testing, and allow receiving results in separate, testable classes independent from your fragment.

void

This method is deprecated.

See onAttach.

void

This method is deprecated.

The responsibility for listening for fragments being attached has been moved to FragmentManager.

boolean

This hook is called whenever an item in a context menu is selected.

void

Called to do initial creation of a fragment.

@Nullable Animation
@MainThread
onCreateAnimation(int transit, boolean enter, int nextAnim)

Called when a fragment loads an animation.

@Nullable Animator
@MainThread
onCreateAnimator(int transit, boolean enter, int nextAnim)

Called when a fragment loads an animator.

void

This method is deprecated.

androidx.activity.ComponentActivity now implements MenuHost, an interface that allows any component, including your activity itself, to add menu items by calling addMenuProvider without forcing all components through this single method override.

void

Called when the fragment is no longer in use.

void

This method is deprecated.

androidx.activity.ComponentActivity now implements MenuHost, an interface that allows any component, including your activity itself, to add menu items by calling addMenuProvider without forcing all components through this single method override.

@NonNull LayoutInflater
onGetLayoutInflater(@Nullable Bundle savedInstanceState)

Returns the LayoutInflater used to inflate Views of this Fragment.

void
@MainThread
onHiddenChanged(boolean hidden)

Called when the hidden state (as returned by isHidden of the fragment or another fragment in its hierarchy has changed.

void
@UiThread
@CallSuper
onInflate(
    @NonNull Activity activity,
    @NonNull AttributeSet attrs,
    @Nullable Bundle savedInstanceState
)

This method is deprecated.

See onInflate.

void
onMultiWindowModeChanged(boolean isInMultiWindowMode)

Called when the Fragment's activity changes from fullscreen mode to multi-window mode and visa-versa.

boolean

This method is deprecated.

androidx.activity.ComponentActivity now implements MenuHost, an interface that allows any component, including your activity itself, to add menu items by calling addMenuProvider without forcing all components through this single method override.

void

This method is deprecated.

androidx.activity.ComponentActivity now implements MenuHost, an interface that allows any component, including your activity itself, to add menu items by calling addMenuProvider without forcing all components through this single method override.

void

Called when the Fragment is no longer resumed.

void
onPictureInPictureModeChanged(boolean isInPictureInPictureMode)

Called by the system when the activity changes to and from picture-in-picture mode.

void

This method is deprecated.

androidx.activity.ComponentActivity now implements MenuHost, an interface that allows any component, including your activity itself, to add menu items by calling addMenuProvider without forcing all components through this single method override.

void
@MainThread
onPrimaryNavigationFragmentChanged(
    boolean isPrimaryNavigationFragment
)

Callback for when the primary navigation state of this Fragment has changed.

void
onRequestPermissionsResult(
    int requestCode,
    @NonNull String[] permissions,
    @NonNull int[] grantResults
)

This method is deprecated.

This method has been deprecated in favor of using the Activity Result API which brings increased type safety via an ActivityResultContract and the prebuilt contracts for common intents available in androidx.activity.result.contract.ActivityResultContracts, provides hooks for testing, and allow receiving results in separate, testable classes independent from your fragment.

void

Called to ask the fragment to save its current dynamic state, so it can later be reconstructed in a new instance if its process is restarted.

void

Called when the Fragment is visible to the user.

void

Called when the Fragment is no longer started.

void

Called when all saved state has been restored into the view hierarchy of the fragment.

void

Postpone the entering Fragment transition until startPostponedEnterTransition or executePendingTransactions has been called.

final void
postponeEnterTransition(long duration, @NonNull TimeUnit timeUnit)

Postpone the entering Fragment transition for a given amount of time and then call startPostponedEnterTransition.

void

Registers a context menu to be shown for the given view (multiple views can show the context menu).

final void
requestPermissions(@NonNull String[] permissions, int requestCode)

This method is deprecated.

This method has been deprecated in favor of using the Activity Result API which brings increased type safety via an ActivityResultContract and the prebuilt contracts for common intents available in androidx.activity.result.contract.ActivityResultContracts, provides hooks for testing, and allow receiving results in separate, testable classes independent from your fragment.

final @NonNull FragmentActivity

Return the FragmentActivity this fragment is currently associated with.

final @NonNull Bundle

Return the arguments supplied when the fragment was instantiated.

final @NonNull Context

Return the Context this fragment is currently associated with.

final @NonNull FragmentManager

This method is deprecated.

This has been renamed to getParentFragmentManager() to make it clear that you are accessing the FragmentManager that contains this Fragment and not the FragmentManager associated with child Fragments.

final @NonNull Object

Return the host object of this fragment.

final @NonNull Fragment

Returns the parent Fragment containing this Fragment.

final @NonNull View

Get the root view for the fragment's layout (the one returned by onCreateView).

void
void
void
void

When custom transitions are used with Fragments, the enter transition callback is called when this Fragment is attached or detached when not popping the back stack.

void
void

When custom transitions are used with Fragments, the exit transition callback is called when this Fragment is attached or detached when popping the back stack.

void
void
setHasOptionsMenu(boolean hasMenu)

This method is deprecated.

This method is no longer needed when using a MenuProvider to provide a Menu to your activity, which replaces onCreateOptionsMenu as the recommended way to provide a consistent, optionally Lifecycle-aware, and modular way to handle menu creation and item selection.

void

Set the initial saved state that this Fragment should restore itself from when first being constructed, as returned by FragmentManager.saveFragmentInstanceState.

void
setMenuVisibility(boolean menuVisible)

Set a hint for whether this fragment's menu should be visible.

void
void
setRetainInstance(boolean retain)

This method is deprecated. Deprecated in Java

void
void
void
void
setTargetFragment(@Nullable Fragment fragment, int requestCode)

This method is deprecated.

Instead of using a target fragment to pass results, the fragment requesting a result should use setFragmentResultListener to register a FragmentResultListener with a requestKey using its parent fragment manager.

void
setUserVisibleHint(boolean isVisibleToUser)

This method is deprecated. Deprecated in Java

boolean

Gets whether you should show UI with rationale before requesting a permission.

void

Call startActivity from the fragment's containing Activity.

void

Call startActivity from the fragment's containing Activity.

void
startActivityForResult(@NonNull Intent intent, int requestCode)

This method is deprecated.

This method has been deprecated in favor of using the Activity Result API which brings increased type safety via an ActivityResultContract and the prebuilt contracts for common intents available in androidx.activity.result.contract.ActivityResultContracts, provides hooks for testing, and allow receiving results in separate, testable classes independent from your fragment.

void
startActivityForResult(
    @NonNull Intent intent,
    int requestCode,
    @Nullable Bundle options
)

This method is deprecated.

This method has been deprecated in favor of using the Activity Result API which brings increased type safety via an ActivityResultContract and the prebuilt contracts for common intents available in androidx.activity.result.contract.ActivityResultContracts, provides hooks for testing, and allow receiving results in separate, testable classes independent from your fragment.

void
startIntentSenderForResult(
    @NonNull IntentSender intent,
    int requestCode,
    @Nullable Intent fillInIntent,
    int flagsMask,
    int flagsValues,
    int extraFlags,
    @Nullable Bundle options
)

This method is deprecated.

This method has been deprecated in favor of using the Activity Result API which brings increased type safety via an ActivityResultContract and the prebuilt contracts for common intents available in androidx.activity.result.contract.ActivityResultContracts, provides hooks for testing, and allow receiving results in separate, testable classes independent from your fragment.

void

Begin postponed transitions after postponeEnterTransition was called.

void

Prevents a context menu to be shown for the given view.

From androidx.pdf.viewer.fragment.PdfViewerFragment
final Uri

The URI of the PDF document to display defaulting to null.

final boolean

Controls whether text search mode is active.

final boolean

Indicates whether the toolbox should be visible.

void
onInflate(
    @NonNull Context context,
    @NonNull AttributeSet attrs,
    Bundle savedInstanceState
)

Called when a fragment is being created as part of a view layout inflation, typically from setting the content view of an activity.

boolean

Called when an external link in the PDF is clicked.

void

Invoked when a problem arises during the loading process of the PDF document.

void

Invoked when the document has been fully loaded, processed, and the initial pages are displayed within the viewing area.

void

Invoked when underlying PdfView implementation has been created.

void
onRequestImmersiveMode(boolean enterImmersive)

Called when the PDF view wants to enter or exit immersive mode based on user's interaction with the content.

void

Called when the fragment is visible to the user and actively running.

final void

The URI of the PDF document to display defaulting to null.

final void
setTextSearchActive(boolean value)

Controls whether text search mode is active.

final void
setToolboxVisible(boolean value)

Indicates whether the toolbox should be visible.

From android.view.View.OnCreateContextMenuListener
void

Called when a context menu for the view is about to be shown.

Public constructors

EditablePdfViewerFragment

Added in 1.0.0-alpha13
public EditablePdfViewerFragment()

Protected constructors

EditablePdfViewerFragment

Added in 1.0.0-alpha13
protected EditablePdfViewerFragment(@NonNull PdfStylingOptions pdfStylingOptions)

Public methods

applyDraftEdits

Added in 1.0.0-alpha13
public final void applyDraftEdits()

Applies all draft edits to the document.

This operation executes asynchronously. The operation will be terminated if EditablePdfViewerFragment is removed from the fragment manager while an applyDraftEdits is in progress. EditablePdfViewerFragment internally disallows editing capabilities during complete operation. Upon completion, either onApplyEditsSuccess or onApplyEditsFailed will be invoked with the result.

Throws
ApplyInProgressException

if another apply operation is already in progress.

hasUnsavedChanges

Added in 1.0.0-alpha13
public final boolean hasUnsavedChanges()

Returns true if there are any draft edits that have not yet been applied to the document, false otherwise.

This can be used to prompt the user to save changes before navigating away, as draft edits will be lost if the fragment is removed from the stack or comes out of edit mode.

isApplyEditsInProgress

Added in 1.0.0-alpha13
public final boolean isApplyEditsInProgress()

Returns true if an applyDraftEdits operation is currently in progress.

See also
applyDraftEdits

isEditModeEnabled

Added in 1.0.0-alpha13
public final boolean isEditModeEnabled()

If true, the fragment is in edit mode, allowing for annotating or editing. If false, the fragment is in viewing mode.

Note: The host is responsible for setting this to false after a write operation is complete.

onApplyEditsFailed

Added in 1.0.0-alpha13
public void onApplyEditsFailed(@NonNull Throwable error)

Callback invoked when applying draft edits has failed.

Parameters
@NonNull Throwable error

The Throwable that caused the failure.

See also
applyDraftEdits

onApplyEditsSuccess

Added in 1.0.0-alpha13
public void onApplyEditsSuccess(@NonNull PdfWriteHandle handle)

Callback invoked when draft edits have been successfully applied to the document.

The host should override this method to perform the write operation. The provided PdfWriteHandle allows writing the document changes to a android.os.ParcelFileDescriptor. The handle must be closed after writing to ensure proper resource cleanup.

After the write operation is complete, the host is responsible for exiting the edit mode by setting isEditModeEnabled to false.

Parameters
@NonNull PdfWriteHandle handle

A PdfWriteHandle to be used for writing the changes to a file.

See also
applyDraftEdits

onAttach

public void onAttach(@NonNull Context context)

Called when a fragment is first attached to its context. onCreate will be called after this.

onCreateView

public View onCreateView(
    @NonNull LayoutInflater inflater,
    ViewGroup container,
    Bundle savedInstanceState
)

Called to have the fragment instantiate its user interface view. This is optional, and non-graphical fragments can return null. This will be called between onCreate and onViewCreated.

A default View can be returned by calling Fragment in your constructor. Otherwise, this method returns null.

It is recommended to only inflate the layout in this method and move logic that operates on the returned View to onViewCreated.

If you return a View from here, you will later be called in onDestroyView when the view is being released.

Parameters
@NonNull LayoutInflater inflater

The LayoutInflater object that can be used to inflate any views in the fragment,

ViewGroup container

If non-null, this is the parent view that the fragment's UI should be attached to. The fragment should not add the view itself, but this can be used to generate the LayoutParams of the view.

Bundle savedInstanceState

If non-null, this fragment is being re-constructed from a previous saved state as given here.

Returns
View

Return the View for the fragment's UI, or null.

onDestroyView

public void onDestroyView()

Called when the view previously created by onCreateView has been detached from the fragment. The next time the fragment needs to be displayed, a new view will be created. This is called after onStop and before onDestroy. It is called regardless of whether onCreateView returned a non-null view. Internally it is called after the view's state has been saved but before it has been removed from its parent.

onDetach

public void onDetach()

Called when the fragment is no longer attached to its activity. This is called after onDestroy.

onEnterEditMode

Added in 1.0.0-alpha13
public void onEnterEditMode()

Callback invoked when EditablePdfViewerFragment enters edit mode. This is triggered when the user begins an edit for example modifying a form field or interaction via toolbox.

This callback can be used by the developers to make any UI changes required when the user enters edit mode, e.g. showing the "Save" button to the user.

onExitEditMode

Added in 1.0.0-alpha13
public void onExitEditMode()

Callback invoked when EditablePdfViewerFragment exits edit mode. This is triggered when the the edit mode is disabled and the fragment completes cleaning up it's edit state.

This callback can be used by the developers to make any UI changes required when the user exits edit mode e.g. hiding the "Save" button.

onViewCreated

public void onViewCreated(@NonNull View view, Bundle savedInstanceState)

Called immediately after onCreateView has returned, but before any saved state has been restored in to the view. This gives subclasses a chance to initialize themselves once they know their view hierarchy has been completely created. The fragment's view hierarchy is not however attached to its parent at this point.

Parameters
@NonNull View view

The View returned by onCreateView.

Bundle savedInstanceState

If non-null, this fragment is being re-constructed from a previous saved state as given here.

setEditModeEnabled

Added in 1.0.0-alpha13
public final void setEditModeEnabled(boolean value)

If true, the fragment is in edit mode, allowing for annotating or editing. If false, the fragment is in viewing mode.

Note: The host is responsible for setting this to false after a write operation is complete.