android:webView 处理方向变化
android:webView handling orientation changes
我想防止 webview 每次在方向改变时重新加载页面。
我已遵循以下教程:
但它每次调用 onCreate()
方法并一次又一次地获取页面。
DisplayResult.java :
public class DisplayResult extends AppCompatActivity
{
protected FrameLayout webViewPlaceholder;
protected WebView webView;
protected Uri url;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.display_result);
url = Uri.parse(getIntent().getStringExtra("Url"));
initUI();
Toast.makeText(DisplayResult.this ,"Oncreate",Toast.LENGTH_SHORT).show();
}
protected void initUI()
{
webViewPlaceholder =(FrameLayout)findViewById(R.id.webViewPlaceholder);
if(webView==null)
{
//webView = (WebView) findViewById(R.id.webview);
webView = new WebView(this);
// webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setSupportZoom(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setDisplayZoomControls(false);
//webView.getSettings().setTextZoom(0);
webView.setWebViewClient(new Callback());
webView.loadUrl(url.toString());
}
webViewPlaceholder.addView(webView);
}
private class Callback extends WebViewClient {
MaterialDialog m ;
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return(false);
}
@Override
public void onPageStarted(WebView view, String url, Bitmap facIcon) {
m=new MaterialDialog.Builder(DisplayResult.this)
.title("Fetching Result")
.content("Please wait...")
.progress(true,0)
.show();
}
@Override
public void onPageFinished(WebView view, String url){
m.cancel();
}
}
@Override
protected void onSaveInstanceState(Bundle outState){
super.onSaveInstanceState(outState);
webView.saveState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
webView.restoreState(savedInstanceState);
}
@Override
public void onConfigurationChanged(Configuration newConfig){
if(webView==null)
{
webViewPlaceholder.removeView(webView);
}
super.onConfigurationChanged(newConfig);
setContentView(R.layout.display_result);
url = Uri.parse(getIntent().getStringExtra("Url"));
initUI();
}
}
和我的布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<FrameLayout
android:id="@+id/webViewPlaceholder"
android:layout_width="fill_parent"
android:layout_height="fill_parent"></FrameLayout>
</LinearLayout>
我还在清单文件中添加了:
<activity
android:name=".DisplayResult"
android:configChanges="keyboard|keyboardHidden|orientation"
android:label="@string/display"
android:parentActivityName=".MainActivity" />
还是不行。谁能帮我解决问题:
我也试过了:
Android Webview Handling Orientation Not Working
@Override
public void onConfigurationChanged(Configuration newConfig){
if(webView==null)
{
webViewPlaceholder.removeView(webView);
}
super.onConfigurationChanged(newConfig);
setContentView(R.layout.display_result);
url = Uri.parse(getIntent().getStringExtra("Url"));
initUI();
}
将其替换为
@Override
public void onConfigurationChanged(Configuration newConfig){
super.onConfigurationChanged(newConfig);
}
您还没有将 screenSize
添加到 configChanges
。
来自Android Docs:
Some device configurations can change during runtime (such as screen orientation, keyboard availability, and language). When such a change occurs, Android restarts the running Activity (onDestroy()
is called, followed by onCreate()
). The restart behavior is designed to help your application adapt to new configurations by automatically reloading your application with alternative resources that match the new device configuration.
并且:
Caution: Beginning with Android 3.2 (API level 13), the "screen size" also changes when the device switches between portrait and landscape orientation. Thus, if you want to prevent runtime restarts due to orientation change when developing for API level 13 or higher (as declared by the minSdkVersion and targetSdkVersion attributes), you must include the "screenSize" value in addition to the "orientation" value. That is, you must declare android:configChanges="orientation|screenSize
. However, if your application targets API level 12 or lower, then your activity always handles this configuration change itself (this configuration change does not restart your activity, even when running on an Android 3.2 or higher device).
考虑到这一点,要防止每次更改方向时调用 onCreate()
,您必须将 android:configChanges="orientation|screenSize"
添加到 AndroidManifest。
我想防止 webview 每次在方向改变时重新加载页面。
我已遵循以下教程:
但它每次调用 onCreate()
方法并一次又一次地获取页面。
DisplayResult.java :
public class DisplayResult extends AppCompatActivity
{
protected FrameLayout webViewPlaceholder;
protected WebView webView;
protected Uri url;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.display_result);
url = Uri.parse(getIntent().getStringExtra("Url"));
initUI();
Toast.makeText(DisplayResult.this ,"Oncreate",Toast.LENGTH_SHORT).show();
}
protected void initUI()
{
webViewPlaceholder =(FrameLayout)findViewById(R.id.webViewPlaceholder);
if(webView==null)
{
//webView = (WebView) findViewById(R.id.webview);
webView = new WebView(this);
// webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setSupportZoom(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setDisplayZoomControls(false);
//webView.getSettings().setTextZoom(0);
webView.setWebViewClient(new Callback());
webView.loadUrl(url.toString());
}
webViewPlaceholder.addView(webView);
}
private class Callback extends WebViewClient {
MaterialDialog m ;
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return(false);
}
@Override
public void onPageStarted(WebView view, String url, Bitmap facIcon) {
m=new MaterialDialog.Builder(DisplayResult.this)
.title("Fetching Result")
.content("Please wait...")
.progress(true,0)
.show();
}
@Override
public void onPageFinished(WebView view, String url){
m.cancel();
}
}
@Override
protected void onSaveInstanceState(Bundle outState){
super.onSaveInstanceState(outState);
webView.saveState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
webView.restoreState(savedInstanceState);
}
@Override
public void onConfigurationChanged(Configuration newConfig){
if(webView==null)
{
webViewPlaceholder.removeView(webView);
}
super.onConfigurationChanged(newConfig);
setContentView(R.layout.display_result);
url = Uri.parse(getIntent().getStringExtra("Url"));
initUI();
}
}
和我的布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<FrameLayout
android:id="@+id/webViewPlaceholder"
android:layout_width="fill_parent"
android:layout_height="fill_parent"></FrameLayout>
</LinearLayout>
我还在清单文件中添加了:
<activity
android:name=".DisplayResult"
android:configChanges="keyboard|keyboardHidden|orientation"
android:label="@string/display"
android:parentActivityName=".MainActivity" />
还是不行。谁能帮我解决问题:
我也试过了:
Android Webview Handling Orientation Not Working
@Override
public void onConfigurationChanged(Configuration newConfig){
if(webView==null)
{
webViewPlaceholder.removeView(webView);
}
super.onConfigurationChanged(newConfig);
setContentView(R.layout.display_result);
url = Uri.parse(getIntent().getStringExtra("Url"));
initUI();
}
将其替换为
@Override
public void onConfigurationChanged(Configuration newConfig){
super.onConfigurationChanged(newConfig);
}
您还没有将 screenSize
添加到 configChanges
。
来自Android Docs:
Some device configurations can change during runtime (such as screen orientation, keyboard availability, and language). When such a change occurs, Android restarts the running Activity (
onDestroy()
is called, followed byonCreate()
). The restart behavior is designed to help your application adapt to new configurations by automatically reloading your application with alternative resources that match the new device configuration.
并且:
Caution: Beginning with Android 3.2 (API level 13), the "screen size" also changes when the device switches between portrait and landscape orientation. Thus, if you want to prevent runtime restarts due to orientation change when developing for API level 13 or higher (as declared by the minSdkVersion and targetSdkVersion attributes), you must include the "screenSize" value in addition to the "orientation" value. That is, you must declare
android:configChanges="orientation|screenSize
. However, if your application targets API level 12 or lower, then your activity always handles this configuration change itself (this configuration change does not restart your activity, even when running on an Android 3.2 or higher device).
考虑到这一点,要防止每次更改方向时调用 onCreate()
,您必须将 android:configChanges="orientation|screenSize"
添加到 AndroidManifest。