防止在Rotate上重新加载WebView - Android Studio

时间:2016-05-02 16:27:14

标签: java android xml webview

当我旋转屏幕时,WebView会重新加载整个页面。所以我在Android_Manifest.xml文件中禁用了旋转,但如果我可以进行旋转,那将非常酷。

SkateTube.java -

package com.example.skatetube.skatetube;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;


public class SkateTube extends AppCompatActivity {
    private WebView mWebView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_skatetube);


        mWebView = (WebView) findViewById(R.id.activity_main_webview);
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        if (savedInstanceState == null)
        {
        mWebView.loadUrl("http://skatetube.sytes.net/");
        mWebView.setWebViewClient(new WebViewClient());

    }



    @Override
    public void onBackPressed() {
        if(mWebView.canGoBack()) {
            mWebView.goBack();
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_skate_tube, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

Android_manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.skatetube.skatetube">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">


        <activity
            android:name=".SkateTube"
            android:theme="@style/AppTheme.NoActionBar"

            android:configChanges="keyboard|keyboardHidden|orientation"
            android:label="State preserving implementation"/>



    </application>

    <uses-permission android:name="android.permission.INTERNET" />

</manifest>

3 个答案:

答案 0 :(得分:20)

必须要做3点。这对我来说是一个完整的解决方案。

  1. AndroidManifest.xml需要在android:configChanges中配置 orientation screenSize 以进行目标活动

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.skatetube.skatetube">
        <application
            <activity android:name=".WebViewActivity"
                android:configChanges="orientation|screenSize">
            </activity>
        </application>
    </manifest>
    
  2. 需要覆盖Activity的 onSaveInstanceState onRestoreInstanceState ,以便在轮换时恢复状态

    @Override
    protected void onSaveInstanceState(Bundle outState)
    {
        super.onSaveInstanceState(outState);
        mWebView.saveState(outState);
    }
    
    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState)
    {
        super.onRestoreInstanceState(savedInstanceState);
        mWebView.restoreState(savedInstanceState);
    }
    
  3. 在屏幕旋转时再次加载网址,使用 savedInstanceState 了解目标活动onCreate()中的当前状态

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        mWebView = (WebView) findViewById(R.id.webView_h5);
        if (savedInstanceState == null) {
            mWebView.loadUrl(url);
        }
    }
    
  4. 错过这三个中的任何一个,我的活动仍在重新加载。

答案 1 :(得分:2)

您需要做的就是覆盖onSaveInstanceStateonRestoreInstanceState

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

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

另一个解决方案可能是将清单中的Activity标记更新为

android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"

答案 2 :(得分:2)

不需要java代码只需复制此代码代替activity

中的Manifest.xml代码
<activity
        android:name=".SkateTube"
        android:theme="@style/AppTheme.NoActionBar"
        android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
        android:label="State preserving implementation"/>