Skip to content

Commit

Permalink
[Android] Potential fix for lifecycle issue (#681)
Browse files Browse the repository at this point in the history
* Potential fix for lifecycle issue

* Updating package version

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
sgong-pdftron and github-actions[bot] authored Feb 20, 2024
1 parent 16afa4a commit 16504cf
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.Lifecycle;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Promise;
Expand Down Expand Up @@ -2218,7 +2219,41 @@ public void requestLayout() {

@Override
protected void prepView() {
super.prepView();
// Create a viewer builder with the specified parameters
buildViewer();
if (mViewerBuilder == null) {
return;
}

Context context = getContext();
Activity activity = null;
if (context instanceof ThemedReactContext) {
activity = ((ThemedReactContext) context).getCurrentActivity();
}
if (activity instanceof AppCompatActivity) {
if (activity.isFinishing() || !((AppCompatActivity) activity).getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED)) {
return;
}
}

if (mPdfViewCtrlTabHostFragment != null) {
mPdfViewCtrlTabHostFragment.onOpenAddNewTab(mViewerBuilder.createBundle(getContext()));
} else {
mPdfViewCtrlTabHostFragment = getViewer();
mPdfViewCtrlTabHostFragment.addHostListener(this);

if (mFragmentManager != null) {
mFragmentManager.beginTransaction()
.add(mPdfViewCtrlTabHostFragment, String.valueOf(getId()))
.commitNowAllowingStateLoss();

View fragmentView = mPdfViewCtrlTabHostFragment.getView();
if (fragmentView != null) {
fragmentView.clearFocus(); // work around issue where somehow new ui obtains focus
addView(fragmentView, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
}
}
}

if (mPdfViewCtrlTabHostFragment != null && mPdfViewCtrlTabHostFragment.getView() == null) {
if (mPdfViewCtrlTabHostFragment instanceof RNPdfViewCtrlTabHostFragment) {
Expand Down Expand Up @@ -3105,6 +3140,10 @@ public void onTabDocumentLoaded(String tag) {
fragment.setReactContext((ReactContext) getContext(), getId());
}

if (getPdfViewCtrl() == null || getToolManager() == null) {
return;
}

// Hide add page annotation toolbar button
if (!mShowAddPageToolbarButton) {
mPdfViewCtrlTabHostFragment.toolbarButtonVisibility(ToolbarButtonType.ADD_PAGE, false);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "react-native-pdftron",
"title": "React Native Pdftron",
"version": "3.0.3-35",
"version": "3.0.3-36",
"description": "React Native Pdftron",
"main": "./lib/index.js",
"typings": "index.ts",
Expand Down

0 comments on commit 16504cf

Please sign in to comment.