Skip to content

Commit 16504cf

Browse files
[Android] Potential fix for lifecycle issue (#681)
* Potential fix for lifecycle issue * Updating package version --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 16afa4a commit 16504cf

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

android/src/main/java/com/pdftron/reactnative/views/DocumentView.java

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import androidx.appcompat.app.AppCompatActivity;
2121
import androidx.fragment.app.Fragment;
2222
import androidx.fragment.app.FragmentManager;
23+
import androidx.lifecycle.Lifecycle;
2324

2425
import com.facebook.react.bridge.Arguments;
2526
import com.facebook.react.bridge.Promise;
@@ -2218,7 +2219,41 @@ public void requestLayout() {
22182219

22192220
@Override
22202221
protected void prepView() {
2221-
super.prepView();
2222+
// Create a viewer builder with the specified parameters
2223+
buildViewer();
2224+
if (mViewerBuilder == null) {
2225+
return;
2226+
}
2227+
2228+
Context context = getContext();
2229+
Activity activity = null;
2230+
if (context instanceof ThemedReactContext) {
2231+
activity = ((ThemedReactContext) context).getCurrentActivity();
2232+
}
2233+
if (activity instanceof AppCompatActivity) {
2234+
if (activity.isFinishing() || !((AppCompatActivity) activity).getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED)) {
2235+
return;
2236+
}
2237+
}
2238+
2239+
if (mPdfViewCtrlTabHostFragment != null) {
2240+
mPdfViewCtrlTabHostFragment.onOpenAddNewTab(mViewerBuilder.createBundle(getContext()));
2241+
} else {
2242+
mPdfViewCtrlTabHostFragment = getViewer();
2243+
mPdfViewCtrlTabHostFragment.addHostListener(this);
2244+
2245+
if (mFragmentManager != null) {
2246+
mFragmentManager.beginTransaction()
2247+
.add(mPdfViewCtrlTabHostFragment, String.valueOf(getId()))
2248+
.commitNowAllowingStateLoss();
2249+
2250+
View fragmentView = mPdfViewCtrlTabHostFragment.getView();
2251+
if (fragmentView != null) {
2252+
fragmentView.clearFocus(); // work around issue where somehow new ui obtains focus
2253+
addView(fragmentView, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
2254+
}
2255+
}
2256+
}
22222257

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

3143+
if (getPdfViewCtrl() == null || getToolManager() == null) {
3144+
return;
3145+
}
3146+
31083147
// Hide add page annotation toolbar button
31093148
if (!mShowAddPageToolbarButton) {
31103149
mPdfViewCtrlTabHostFragment.toolbarButtonVisibility(ToolbarButtonType.ADD_PAGE, false);

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "react-native-pdftron",
33
"title": "React Native Pdftron",
4-
"version": "3.0.3-35",
4+
"version": "3.0.3-36",
55
"description": "React Native Pdftron",
66
"main": "./lib/index.js",
77
"typings": "index.ts",

0 commit comments

Comments
 (0)