[Android] Out of memory when rotating the device multiple times

72 views
Skip to first unread message

Anderson Konzen

unread,
May 8, 2014, 1:19:52 PM5/8/14
to pdfne...@googlegroups.com
Q: 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.

--------

A: This happens because every time you rotate the device the activity is recreated and the current instance of PDFViewCtrl is not being finalized correctly, ie., onCreate will be called again and a new PDFViewCtrl will be instantiated, etc. One way to remedy this is to call PDFViewCtrl.destroy() on your activity:
    
    @Override
    protected void onDestroy() {
        mPdfCtrl.destroy();
        mPdfCtrl = null;
        super.onDestroy();
    }


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. To do this you just need to add in the manifest file the following:
android:configChanges="keyboardHidden|orientation|screenSize"

For example:
<activity
    android:name="com.example.MainActivity"
    android:configChanges="keyboardHidden|orientation|screenSize"
    android:label="@string/app_name" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

You can then override onConfigurationChanged if you need to pass this info to the ToolManager:

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        mPdfCtrl.onConfigurationChanged(newConfig);
    }
Reply all
Reply to author
Forward
0 new messages