Out of memory when rotating device multiple times

Question:

We are experiencing a lot of out of memory errors when rotating the device multiple times. Since the Activity is destroyed while rotating, we get a new (quite heavy) PDFViewCtrl every time we rotate the device.

When you rotate the device, the activity is recreated, and the current state of PDFViewCtrl is not being finalized correctly. For example, when rotating a device, Android called onCreate(), your activity therefore instantiates a new PDFViewCtrl. The following options could help you get rid of this problem.

Option 1: Overriding onDestroy() in your activity

When overriding onDestroy(), call PDFViewCtrl.destroy():

public class MyActivity extends AppCompatActivity {
    ...
    @Override
    protected void onDestroy() {
        mPdfCtrl.destroy();
        mPdfCtrl = null;
        super.onDestroy();
    }
}

where mPdfCtrl is an instance of PDFViewCtrl.

Option 2: Configuring your activity to handle configuration changes.

Another way to remedy this is configuring your activity to handle configuration changes. This way the control will not be recreated and it will be resized accordingly.

  1. In your app AndroidManifest.xml file, add android:configChanges to your activity tag.

    ...
      <activity
        ...
        android:configChanges="keyboardHidden|orientation|screenSize|screenLayout|smallestScreenSize"
        >
        ...
      </activity>
    ...
  2. Override onConfigurationChanged() in your activity, and call PDFViewCtrl.onConfigurationChanged(Configuration):

    public class MyActivity extends AppCompatActivity {
        ...
        @Override
        public void onConfigurationChanged(Configuration newConfig) {
            super.onConfigurationChanged(newConfig);
            mPdfCtrl.onConfigurationChanged(newConfig);
        }
    }

    where mPdfCtrl is an instance of PDFViewCtrl.