2012-07-12 67 views
1

我使用下面的代碼,以阻止重新加載網頁Android的方向變化的WebView

public class MainActivity extends Activity { 


WebView webView; 

@Override 


protected void onSaveInstanceState(Bundle outState) { 


    WebView webView1 = (WebView)findViewById(R.id.webView); 
    webView1.saveState(outState); 


}  

@SuppressLint("SetJavaScriptEnabled") 
@Override 
public void onCreate(Bundle savedInstanceState) { 


    super.onCreate(savedInstanceState); 

    setContentView(R.layout.activity_main); 

    webView = (WebView)findViewById(R.id.webView); 

    String url="http://www.google.com"; 


      if (savedInstanceState != null) 
         { 
       ((WebView)findViewById(R.id.webView)).restoreState(savedInstanceState); 
         } 
      else{ 

      webView.setWebViewClient(new WebViewClient() { 


     public void onPageFinished(WebView view, String url) { 

      String name = MainActivity.this.webView.getTitle(); 
      TextView t=(TextView)findViewById(R.id.title); 

      t.setText(name); 
      Toast.makeText(getApplicationContext(), name, Toast.LENGTH_SHORT).show(); 


     } 
    }); 



    final Activity activity = this; 

    final ProgressDialog progressDialog = new ProgressDialog(activity); 
    progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
    progressDialog.setMessage("Loading..."); 
    progressDialog.setCancelable(false); 

    final ProgressBar progressBar = (ProgressBar)findViewById(R.id.progressBar1); 


    webView.getSettings().setJavaScriptEnabled(true); 
    webView.getSettings().setDomStorageEnabled(true); 
    webView.getSettings().setUserAgentString("Android"); 
    webView.setWebChromeClient(new WebChromeClient() 
    { 
     public void onProgressChanged(WebView view, int progress) { 

       progressBar.setVisibility(View.VISIBLE); 
       progressDialog.setProgress(0); 
       activity.setProgress(progress * 1000); 

       progressDialog.incrementProgressBy(progress); 

       if(progress > 75) 
        progressBar.setVisibility(View.GONE); 
      } 
    } 
      ); 


    webView.loadUrl(url); 

      } 

,並在我的清單文件

使用android:configChanges="orientation|keyboard|keyboardHidden"但是當我運行它,並改變我的手機的方向。

頁面仍然重新加載並且進度條開始顯示並且從不關閉。

任何人都可以告訴我這是怎麼回事?

+0

您可以發佈您的佈局xml文件也。所以同時我會嘗試測試你的代碼。 – 2012-07-14 16:36:39

+0

此代碼正在使用薑餅,但無法在ICS上工作 – Badal 2012-07-14 17:40:45

+0

ICS OS 4.0.0有一些問題。嘗試使用OS 4.0.3或其他操作系統。 – 2012-07-15 07:09:53

回答

1

默認情況下,活動在定位更改時重新創建。但是您可以通過在AndroidManifest.xml中聲明您的活動並設置活動類的方法來設置android:configChanges="keyboardHidden|orientation來更改它。

+0

我已經這樣做了,我已經編輯了我的問題 – Badal 2012-07-12 09:41:08

+0

你在'onConfigurationChanged()'方法中放了什麼? – fiddler 2012-07-12 09:43:08

+0

'super.onConfigurationChanged(newConfig);' – Badal 2012-07-12 09:44:06

0

您需要使用configChanges屬性作爲清單文件中的Activity。 因此,在每個活動的Manifest文件中使用下面的代碼。

<activity android:name=".YOUR_ACTIVITY_NAME" android:configChanges="orientation|keyboard|keyboardHidden"/> 

當你不使用configChanges然後onCreate()方法再次調用當設備方向更改或其他配置更改。

+0

我已經做到了,現在我已經編輯了我的問題。 – Badal 2012-07-12 09:42:59

+0

評論或從您的活動代碼 – 2012-07-12 09:44:52

+0

刪除onConfigurationChanged()也試過。 – Badal 2012-07-12 09:45:34

1

只是把這個在您的清單活動

android:configChanges="keyboardHidden|orientation" 

它的工作對我來說

1

請添加屏幕尺寸也在清單機器人:configChanges =「方向|鍵盤|屏幕尺寸」

0

在我其中WebView在片段中的代碼:

In manifest:

<activity 
... 
android:configChanges="orientation|keyboard|screenSize" 
... /> 

在活動:

@Override 
public void onConfigurationChanged(Configuration newConfig) { 
    super.onConfigurationChanged(newConfig); 
} 

在片段:

@Override 
public void onSaveInstanceState(Bundle outState) { 
    super.onSaveInstanceState(outState); 
    webView.saveState(outState); 
} 
@Override 
public void onActivityCreated(Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 
    if (savedInstanceState == null) 
     webView.loadUrl("about:blank"); 
    else 
     webView.restoreState(savedInstanceState); 
} 

,當方向改變它的平滑地改變。

1

試試這個

在活動標籤,艙單

android:screenOrientation="sensor" 
     android:configChanges="keyboardHidden|orientation|screenSize" 

的Java

@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. 
}