Skip to content

Commit

Permalink
Fix a potential NPE Crash due to Mismatched Thread Lifecycle
Browse files Browse the repository at this point in the history
Fix a potential NPE due to out of sync render thread management [Pull Request](DImuthuUpe/AndroidPdfViewer#824)
Update the sample app configuration so that it can compile [Pull Request](mhiew/AndroidPdfViewer#2)
  • Loading branch information
AhmerAfzal1 committed Feb 14, 2022
1 parent 9069e7f commit 422c014
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion PdfViewer/src/main/java/com/ahmer/pdfviewer/PDFView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,7 @@ class PDFView(context: Context?, set: AttributeSet?) : RelativeLayout(context, s
fun loadComplete(pdfFile: PdfFile) {
mState = State.LOADED
this.pdfFile = pdfFile
if (mRenderingHandlerThread == null) return
if (mRenderingHandlerThread?.isAlive != true) mRenderingHandlerThread?.start()
renderingHandler = mRenderingHandlerThread?.looper?.let {
RenderingHandler(it, this)
Expand Down Expand Up @@ -973,6 +974,11 @@ class PDFView(context: Context?, set: AttributeSet?) : RelativeLayout(context, s
mAnimationManager?.computeFling()
}

override fun onAttachedToWindow() {
super.onAttachedToWindow()
if (mRenderingHandlerThread == null) mRenderingHandlerThread = HandlerThread("PDF renderer")
}

override fun onDetachedFromWindow() {
recycle()
if (mRenderingHandlerThread != null) {
Expand Down Expand Up @@ -1325,7 +1331,6 @@ class PDFView(context: Context?, set: AttributeSet?) : RelativeLayout(context, s
}

private fun initPDFView() {
mRenderingHandlerThread = HandlerThread("PDF renderer")
if (isInEditMode) return
mPdfiumCore = PdfiumCore(context)
cacheManager = CacheManager()
Expand Down

0 comments on commit 422c014

Please sign in to comment.