外部键盘连接时如何保存WebView状态?
问题描述:
当我使用OTG(A4TECH GX-110)连接外部键盘时,我的Activity正在从头开始创建。这导致重新创建WebView
。外部键盘连接时如何保存WebView状态?
这是我的XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context="uz.cp.oxmini.MainActivity">
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/wvOx" />
</RelativeLayout>
我试图挽救WebView
状态,然后onCreate()
方法来恢复已保存的状态。
private WebView mWebview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mWebview = new WebView(this);
if (savedInstanceState != null){
mWebview.restoreState(savedInstanceState);
}
else {
mWebview.loadUrl("http://joerichard.net");
setContentView(mWebview);
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
mWebview.saveState(outState);
}
但结果是屏幕灰色背景
问题是什么?如何解决它?
答
我已经添加onRestoreInstanceState并修改onSaveInstance方法一点点。现在我正在恢复onRestoreInstanceState中的webview状态。而且我已经改变了检查savedInstanceState为空或不是:
private WebView mWebview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mWebview = new WebView(this);
if (savedInstanceState == null) {
mWebview.loadUrl("http://joerichard.net");
}
setContentView(mWebview);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mWebview.saveState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
mWebview.restoreState(savedInstanceState);
}
哪里是你的xml文件 –
@RohanPawar,我不知道这是如何能帮助,还是保留了,你可以在编辑的问题,找到它 –