2013-07-17 101 views
0

當我旋轉屏幕的網頁重新加載,但沒有我原來的標題。Webview方向改變webview丟失標題

我恢復使用web視圖狀態:

@Override 
public void onActivityCreated(Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 
    if (savedInstanceState == null) { 
     // Restore the state of the WebView 
     // webView.getRefreshableView().restoreState(savedInstanceState); 
     webView.loadUrl("my_url"); 
    } else { 
     webView.restoreState(savedInstanceState); 
    } 
} 

@Override 
public void onSaveInstanceState(Bundle outState) { 
    super.onSaveInstanceState(outState); 
    webView.saveState(outState); 
} 

任何想法上的解決方案?我真的想恢復狀態,因爲它包含了所有的網絡歷史記錄,但是當頁面重新加載時我需要標題!

+0

嘗試'的onResume裝載頭()' –

+0

@RohanKandwal你這是什麼意思,裝載頭在onResume?據我所見,你必須在webview.onLoad(url,headers)中指定標題 – serenskye

+0

你可以做的是將你的標題保存在'saveInstanceState'中,並在'onResume'中恢復標題 –

回答

1

我固定它使用此解決方案: http://www.devahead.com/blog/2012/01/preserving-the-state-of-an-android-webview-on-screen-orientation-change/

現在doen'st重新加載頁面,只需重新呈現這樣頭,其中包括,網絡歷史記錄也完好無損。

建議here

我修改了它的片段,片段這裏工作:

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    this.setRetainInstance(true); 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.web_fragment, null); 
    //webContainer =() view.findViewById(R.id.web_container); 
    initUI(view); 
    setupWebView(); 
    return view; 
} 

protected void initUI(View view) 
{ 
    // Retrieve UI elements 
    webViewPlaceholder = ((FrameLayout)view.findViewById(R.id.webViewPlaceholder)); 
    // Initialize the WebView if necessary 
    if (webView == null) 
    { 
     // Create the webview 
     webView = new JSWebView(getActivity()); 
     webView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT)); 

     // Load the URLs inside the WebView, not in the external web browser 
     webView.setWebViewClient(new WebViewClient()); 

     // Load a page 
     webView.loadUrl("http://someinitialurl.com"); 
    } 

    // Attach the WebView to its placeholder 
    webViewPlaceholder.addView(webView); 
} 

@Override 
public void onConfigurationChanged(Configuration newConfig) 
{ 
    if (webView != null) 
    { 
     // Remove the WebView from the old placeholder 
     webViewPlaceholder.removeView(webView); 
    } 

    super.onConfigurationChanged(newConfig); 

    // Reinitialize the UI 
    initUI(getView()); 
}