建议轮换再次收取我的webviews类

时间:2012-05-17 07:58:38

标签: android webview rotation orientation

当我旋转我的设备时,这又为他的课程充电。例如,如果我再次将定向费用更改为Google,则会进行Google搜索。

我尝试过:

@Override
public void onConfigurationChanged(Configuration newConfig) {
  super.onConfigurationChanged(newConfig);
  setContentView(R.layout.principal);
}

和onRestoreInstanceState和onSaveInstanceState。但我的问题仍然存在。如果有人可以帮助我做一个例子或说明如何做到这一点,我将不胜感激。

感谢的!


我已经解决了这个问题,我需要做if(savedInstanceState == null):

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.principal);

    webview.setWebViewClient(new WebViewClient());

    if (savedInstanceState == null)
    {
        webview.loadUrl("http://www.apple.es");
    }
}

  @Override   
    protected void onSaveInstanceState(Bundle outState)
    {  
      super.onSaveInstanceState(outState);  
      // Save the state of the WebView    
      webview.saveState(outState);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState)  
    {  
      super.onRestoreInstanceState(savedInstanceState);
      // Restore the state of the WebView
      webview.restoreState(savedInstanceState);
    }

我希望比我的问题和我的解决方案对某人有所帮助!

3 个答案:

答案 0 :(得分:0)

在AndroidManifest文件中添加configChanges。 例如:

<activity android:name="YourActivityName"
          android:configChanges="orientation">
</activity>

并将当前网址保存在本地变量中并将其加载到onConfigChanges

@Override
public void onConfigurationChanged(Configuration newConfig) {
    yourWebView.loadUrl(yourUrl);
}

答案 1 :(得分:0)

使用以下代码

public void onConfigurationChanged(Configuration newConfig) {
    yourWebView.loadUrl(yourUrl);
}

请参阅此链接 Activity restart on rotation Android

答案 2 :(得分:0)

Christian你必须在android开发者网站上看到this doc并了解romain guy如何存储他上次加载的图像并在配置屏幕更改时调用它们!解决方案是使用

  1. onRetainNonConfigurationInstance()
  2. getLastNonConfigurationInstance()
  3. 我也指的是这个tutorial总是摆脱android的屏幕变化行为

    希望这会对你有所帮助