简体   繁体   English

避免在方向更改时使Webview重新呈现网页[Android]

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

I am trying to avoid webview re-render web page on orientation change. 我试图避免在方向改变时webview重新呈现网页。

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. 当我更改活动方向时,我的Web视图需要花费一些时间来渲染/刷新页面。 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. 我的Web视图正在呈现的网页是本地的,仍然需要一些时间(大约2-4秒)来显示页面的内容,并且在过渡时间内我看到空白的白屏。

I tried to save the state of the web view by calling following functions: 我试图通过调用以下函数来保存Web视图的状态:

 // 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. 但是,正如android sdk文档中所提到的,我们需要在活动周期的onSaveInstanceStateonRestoreInstanceState中调用这些函数,因此在这种情况下,由于我没有从活动周期调用这些函数,因此我在此崩溃。

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. 更新:在我的应用程序中,我正在使用设置方向,因此我无法使用onSaveInstanceStateonRestoreInstanceState ,因为当我们具有固定方向时,不会调用这些函数。

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. 这将防止系统在更改方向时重新创建您的“活动”。

我认为您应该在文档的此部分中查看: 自行处理配置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM