How to invoke PdfActivity share action programatically

If you want to move the share action from the main toolbar of the PdfActivity, you can use the following method from your custom activity that is extending the PdfActivity.

First, get hold of the share menu item and remove it from the main toolbar:

Copy
CustomActivity.kt
1
2
3
4
5
6
7
8
9
10
11
12
override fun onCreateOptionsMenu(menu: Menu): Boolean {
    // This populates the menu with main toolbar items.
    super.onCreateOptionsMenu(menu)

    // Store the share menu item for future use.
    shareMenuItem = menu.findItem(PdfActivity.MENU_OPTION_SHARE)

    // Then remove the share item from the main toolbar.
    if (shareMenuItem != null) {
        menu.removeItem(PdfActivity.MENU_OPTION_SHARE)
    }
}

Then, invoke the share action by calling:

CustomActivity.kt
1
2
3
if (shareMenuItem != null) {
    onOptionsItemSelected(shareMenuItem)
}