How to fix rotation on webview for Android phones?

后端 未结 4 1254
野性不改
野性不改 2021-01-15 04:48

I\'m trying to figure out how to fix the orientation problem on webview. Basically every time the user changes the orientation on the device the program goes white and reloa

4条回答
  •  醉酒成梦
    2021-01-15 05:33

    I had the same problem. The reason that the webView restarts when you flip the screen (in my case at least) is that you always initialize the webView when onCreate() runs, even though the webView is not null.

    First add the following in your AndroidManifest.xml:

    
    

    Then, try adding an if-clause:

    ...
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        //Add this
        if(webView == null){
            webView = (WebView)findViewById(R.id.webView);
            WebSettings webSettings =webView.getSettings();
            webSettings.setJavaScriptEnabled(true);
            webView.setWebViewClient (new HelloWebViewClient());
            webView.loadUrl("http://google.com");
        }
    
        if (savedInstanceState != null)
            ((WebView)findViewById(R.id.webView)).restoreState(savedInstanceState);
    }
    
    ...
    

提交回复
热议问题