User7723337
User7723337

Reputation: 12018

Avoid webview re-render web page on orientation change [Android]

I am trying to avoid webview re-render web page on orientation change.

My activity is having fixed orientation as portrait, but on one button click I am changing orientation of the activity to landscape and portrait.

When I change the orientation of the activity then my web view takes time to render/refresh the page. My web page that web view is rendering is local and still it takes time (around 2-4 seconds) to display the contains of the page and i am seeing blank white screen in the transition time.

I tried to save the state of the web view by calling following functions:

 // Before switching calling
 Bundle webViewState;
 webViewState = new Bundle();
 Webview.saveState(webViewState);

 // When switching done then calling. 
 Webview.restoreState(webViewState);

But as mentioned in the android sdk docs we need to call these function in onSaveInstanceState and onRestoreInstanceState of activity cycle, so i am getting crash in this case as i am not calling these functions from activity cycle.

Does anyone have workaround/fix for this case.

UPDATE: In my application i am using set orientation so i am not able to use onSaveInstanceState and onRestoreInstanceState, as these functions are not getting called when we have fixed orientation.

Upvotes: 0

Views: 1310

Answers (2)

XeNoN
XeNoN

Reputation: 696

Page is re-rendered because your activity is destroyed and recreated on orientation change. Update your activity in manifest:

<activity android:name="..."
    android:configChanges="orientation|screenSize"  .../>

This will prevent system to recreate your Activity when orientation is changed.

Upvotes: 1

pshegger
pshegger

Reputation: 2596

I think you should have a look in this section of the documentation: Handling the Configuration Change Yourself

Upvotes: 0

Related Questions