gle*_*nyb 145 android orientation webview
问题是旋转后的性能.WebView必须重新加载页面,这可能有点单调乏味.
每次处理方向更改而不从源重新加载页面的最佳方法是什么?
Tot*_*ach 88
如果您不希望WebView在方向更改时重新加载,只需覆盖Activity类中的onConfigurationChanged:
@Override
public void onConfigurationChanged(Configuration newConfig){
super.onConfigurationChanged(newConfig);
}
Run Code Online (Sandbox Code Playgroud)
并在清单中设置android:configChanges属性:
<activity android:name="..."
android:label="@string/appName"
android:configChanges="orientation|screenSize"
Run Code Online (Sandbox Code Playgroud)
有关详细信息,请参阅:http:
//developer.android.com/guide/topics/resources/runtime-changes.html#HandlingTheChange
https://developer.android.com/reference/android/app/Activity.html#ConfigurationChanges
小智 69
编辑:此方法不再按照文档中的说明运行
原始答案:
这可以通过覆盖onSaveInstanceState(Bundle outState)
您的活动并saveState
从Webview 调用来处理:
protected void onSaveInstanceState(Bundle outState) {
webView.saveState(outState);
}
Run Code Online (Sandbox Code Playgroud)
然后在webview重新充气之后在你的onCreate中恢复它:
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.blah);
if (savedInstanceState != null)
((WebView)findViewById(R.id.webview)).restoreState(savedInstanceState);
}
Run Code Online (Sandbox Code Playgroud)
Tin*_*she 21
最好的答案是遵循此处的 Android文档 基本上这将阻止Webview重新加载:
<activity android:name=".MyActivity"
android:configChanges="keyboardHidden|orientation|screenSize|layoutDirection|uiMode"
android:label="@string/app_name">
Run Code Online (Sandbox Code Playgroud)
在活动中:
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试使用onRetainNonConfigurationInstance(返回WebView),然后在onCreate期间使用getLastNonConfigurationInstance将其恢复并重新分配它.
似乎还没有工作.我不禁想到我真的很亲密!到目前为止,我只是得到一个空白/白色背景WebView.在这里张贴希望有人可以帮助推动这一个超越终点线.
也许我不应该通过WebView.也许是WebView中的一个对象?
我尝试的另一种方法 - 不是我最喜欢的 - 是在活动中设置它:
android:configChanges="keyboardHidden|orientation"
Run Code Online (Sandbox Code Playgroud)
......然后在这里做什么都没有:
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// We do nothing here. We're only handling this to keep orientation
// or keyboard hiding from causing the WebView activity to restart.
}
Run Code Online (Sandbox Code Playgroud)
这有效,但它可能不被认为是最佳做法.
同时,我也有一个ImageView,我想根据轮换自动更新.事实证明这很容易.在我的res
文件夹下,我拥有drawable-land
并drawable-port
保持横向/纵向变化,然后我R.drawable.myimagename
用于ImageView的源和Android"做正确的事" - 耶!
...除非你观察配置更改,否则它不会.:(
所以我很不相干.使用onRetainNonConfigurationInstance和ImageView旋转工作,但WebView持久性不...或使用onConfigurationChanged并且WebView保持稳定,但ImageView不会更新.该怎么办?
最后一点:在我的情况下,强迫定位不是一个可接受的妥协.我们确实希望优雅地支持旋转.有点像Android浏览器应用程序的功能!;)
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle state) {
super.onRestoreInstanceState(state);
}
Run Code Online (Sandbox Code Playgroud)
这些方法可以在任何活动上重写,它基本上允许您在每次创建/销毁活动时保存和恢复值,当屏幕方向更改时,活动会在后台被销毁并重新创建,因此您可以使用这些方法在更改期间临时存储/恢复状态。
您应该更深入地研究以下两种方法,看看它是否适合您的解决方案。
http://developer.android.com/reference/android/app/Activity.html
归档时间: |
|
查看次数: |
89208 次 |
最近记录: |