当我旋转屏幕时,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>
答案 0 :(得分:20)
必须要做3点。这对我来说是一个完整的解决方案。
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>
需要覆盖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);
}
不在屏幕旋转时再次加载网址,使用 savedInstanceState 了解目标活动onCreate()中的当前状态
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
mWebView = (WebView) findViewById(R.id.webView_h5);
if (savedInstanceState == null) {
mWebView.loadUrl(url);
}
}
错过这三个中的任何一个,我的活动仍在重新加载。
答案 1 :(得分:2)
您需要做的就是覆盖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);
}
另一个解决方案可能是将清单中的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"/>