【问题标题】:Prevent Chrome from reloading page on re-orientation?防止 Chrome 在重新定位时重新加载页面?
【发布时间】:2012-11-14 20:34:23
【问题描述】:

这是我的第一个应用程序,它是一个网络应用程序,它只是将 webView 加载到发生事情的 url。

它在 android 浏览器(姜饼)中运行良好,但在 Chrome(ICS,JB)中,当设备旋转时,它会返回到初始 webView url。在姜饼中,您可以通过设置 android:configChanges="keyboard|keyboardHidden|orientation" 并覆盖 onConfigurationChanged 来解决此问题。

在网上搜索后,我根据此处的示例重新编写了我的原始活动:http://www.devahead.com/blog/2012/01/preserving-the-state-of-an-android-webview-on-screen-orientation-change/,在其中您基本上可以自己处理重新定向。

但是现在我的应用在我旋转设备时崩溃了。

使用 Android 4.1.2 进行测试。

有人看到我的代码有什么问题吗?对不起,如果答案很明显,但我已经被这个错误困住了几天了。也许我只需要另一双眼睛。提前致谢!

注意:@SuppressWarnings("deprecation") 行不在示例中,这是编辑器为了编译而建议的。

主要活动:

package com.example.xxx.xxx;

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.content.res.Configuration;
import android.view.Menu;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;

@SuppressLint("SetJavaScriptEnabled")
public class MainActivity extends Activity {

protected FrameLayout webViewPlaceholder;
protected WebView myWebView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    initUI();

}

   @SuppressWarnings("deprecation")
   protected void initUI() {

    webViewPlaceholder = ((FrameLayout)findViewById(R.id.webViewPlaceholder));


    if (myWebView == null){
        //Create It
        myWebView = new WebView(this);
        myWebView.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setSupportZoom(true);
        webSettings.setBuiltInZoomControls(true);
        webSettings.setLoadsImagesAutomatically(true);
        myWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
        myWebView.setScrollbarFadingEnabled(true);
        myWebView.setWebViewClient(new WebViewClient());
        myWebView.setWebChromeClient(new WebChromeClient());
        myWebView.setInitialScale(1);
        myWebView.loadUrl("http://www.theurl.com");

    }

    webViewPlaceholder.addView(myWebView);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

@Override
public void onConfigurationChanged(Configuration newConfig){   

    if (myWebView != null) {

        webViewPlaceholder.removeView(myWebView);

    }

    super.onConfigurationChanged(newConfig);

    setContentView(R.layout.activity_main);

    initUI();

}


@Override
protected void onSaveInstanceState(Bundle outState)
      {
        super.onSaveInstanceState(outState);

        // Save the state of the WebView
        myWebView.saveState(outState);
      }


@Override
protected void onRestoreInstanceState(Bundle savedInstanceState)
      {
        super.onRestoreInstanceState(savedInstanceState);

        // Restore the state of the WebView
        myWebView.restoreState(savedInstanceState);
      }
}

主要活动 XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:orientation="horizontal" >

<FrameLayout android:id="@+id/webViewPlaceholder"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"/>

</LinearLayout>

【问题讨论】:

  • 我刚刚更新到 Android 4.2 分钟后,状态的保存/加载现在工作。现在我需要弄清楚如何保持我的表单数据完好无损。有什么想法吗?

标签: android webview android-webview onconfigurationchanged webchromeclient


【解决方案1】:

尝试在清单文件中为您的活动定义添加方向,如下所示:

<activity android:screenOrientation="portrait"  ...>
</activity>

【讨论】:

  • 感谢您的回复。我尝试了您的建议,但它只是锁定了方向。这是解决此问题的一种解决方法,但我希望允许用户轮换并仍保留他们迄今为止输入的任何表单数据。编辑:对不起,我没有足够的代表来投票。
【解决方案2】:

我意识到,如果我不使用 WebChromeClient 的任何功能,我什至根本不需要它。在这里找到答案:How to prevent WebView auto refresh when screen rotation

只需在清单中的配置更改中添加“screenSize”即可。

android:configChanges="keyboard|keyboardHidden|orientation|screenSize"

【讨论】:

    猜你喜欢
    • 2018-07-04
    • 1970-01-01
    • 2017-06-26
    • 2014-04-16
    • 2013-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多