googlearchive/android-PdfRendererBasic

App crashes when minimize the App and Open App again

Closed this issue · 4 comments

Hi, App goes to crash when I minimize App and then open
The app from minimized state.

Android Version : 7.0
Android Device : MOTO C Plus

FATAL EXCEPTION: main Process: com.example.android.pdfrendererbasic, PID: 32027 java.lang.RuntimeException: Unable to resume activity {com.example.android.pdfrendererbasic/com.example.android.pdfrendererbasic.MainActivity}: java.lang.IllegalStateException: Already closed at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3506) at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3546) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1577) at android.os.Handler.dispatchMessage(Handler.java:110) at android.os.Looper.loop(Looper.java:203) at android.app.ActivityThread.main(ActivityThread.java:6251) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1075) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936) Caused by: java.lang.IllegalStateException: Already closed at android.graphics.pdf.PdfRenderer$Page.throwIfClosed(PdfRenderer.java:423) at android.graphics.pdf.PdfRenderer$Page.close(PdfRenderer.java:398) at com.example.android.pdfrendererbasic.PdfRendererBasicFragment.showPage(PdfRendererBasicFragment.java:197) at com.example.android.pdfrendererbasic.PdfRendererBasicFragment.onStart(PdfRendererBasicFragment.java:122) at android.support.v4.app.Fragment.performStart(Fragment.java:2380) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1458) at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1740) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1809) at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3217) at android.support.v4.app.FragmentManagerImpl.dispatchStart(FragmentManager.java:3176) at android.support.v4.app.FragmentController.dispatchStart(FragmentController.java:203) at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570) at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:177) at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1248) at android.app.Activity.performStart(Activity.java:6683) at android.app.Activity.performRestart(Activity.java:6757) at android.app.Activity.performResume(Activity.java:6762) at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3477) at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3546) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1577) at android.os.Handler.dispatchMessage(Handler.java:110) at android.os.Looper.loop(Looper.java:203) at android.app.ActivityThread.main(ActivityThread.java:6251) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1075) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)

@abhay222 I have the same problem with you, and i solved it like this

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mPageIndex = 0;
        if (null != savedInstanceState) {
            mPageIndex = savedInstanceState.getInt(STATE_CURRENT_PAGE_INDEX, 0);
        }
        showPage(mPageIndex);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        try {
            openRenderer(context);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @Override
    public void onDestroy() {
        try {
            closeRenderer();
        } catch (IOException e) {
            e.printStackTrace();
        }
        super.onDestroy();
    }

openRenderer in onAttach() instead of onStart() , and showPage(mPageIndex) in onViewCreated method, closeRenderer in onDestroy()

Thanks @rickyin

Thanks a lot @rickyin ... Your solution is perfect :)

I am closing this issue/PR, as it has been migrated to the new repo linked above in the comments. Thank you!