2013-11-21 119 views
0

我想要在斷片中保存狀態的webview。我想要的是。當我通過導航抽屜切換片段時,我想在片段的webview中保存狀態。我發現了一些代碼來保存狀態,但實際上我不知道在哪裏寫它。有人可以幫我解決這個問題嗎?我爲我的英語抱歉..在片段中保存狀態WebView

這裏是saveState和

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

@Override 
public void onActivityCreated(Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 
    mWebView.restoreState(savedInstanceState); 
} 

代碼這是我的代碼

public class Zastupovanie extends SherlockFragment{ 




    Context context; 



     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup group, Bundle saved) 
      { 
      View v = inflater.inflate(R.layout.zastupovanie, group, false); 
      final ProgressDialog pd = ProgressDialog.show(getActivity(), "", "Prosím čakajte. Prebieha načítavanie... ", true); 

      final WebView myWebView = (WebView) v.findViewById(R.id.zastupovanie2); 
      myWebView.setWebViewClient(new WebViewClient() { 
       @Override 
       public void onPageFinished(WebView view, String url) { 
        pd.dismiss(); 
       } 
      }); 

      myWebView.getSettings().setBuiltInZoomControls(false); 
      myWebView.getSettings().setSupportZoom(false); 
      myWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true); 
      myWebView.getSettings().setJavaScriptEnabled(true); 
      myWebView.getSettings().setAllowFileAccess(true); 
      myWebView.getSettings().setDomStorageEnabled(true); 

      myWebView.getSettings().setDefaultZoom(ZoomDensity.FAR); 

      myWebView.loadUrl("http://soszm.edupage.org/substitution/?"); 

      return v; 
      } 

     } 

回答

1

你可以簡單地這樣的方法添加到您的片段是這樣的:

public class Zastupovanie extends SherlockFragment{ 

    Context context; 
    private WebView myWebView 

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

    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 
     myWebView.restoreState(savedInstanceState); 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup group, Bundle saved) 
     { 
     View v = inflater.inflate(R.layout.zastupovanie, group, false); 
     final ProgressDialog pd = ProgressDialog.show(getActivity(), "", "Prosím čakajte. Prebieha načítavanie... ", true); 

     myWebView = (WebView) v.findViewById(R.id.zastupovanie2); 
     myWebView.setWebViewClient(new WebViewClient() { 
      @Override 
      public void onPageFinished(WebView view, String url) { 
       pd.dismiss(); 
      } 
     }); 

     myWebView.getSettings().setBuiltInZoomControls(false); 
     myWebView.getSettings().setSupportZoom(false); 
     myWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true); 
     myWebView.getSettings().setJavaScriptEnabled(true); 
     myWebView.getSettings().setAllowFileAccess(true); 
     myWebView.getSettings().setDomStorageEnabled(true); 

     myWebView.getSettings().setDefaultZoom(ZoomDensity.FAR); 

     myWebView.loadUrl("http://soszm.edupage.org/substitution/?"); 

     return v; 
     } 

    } 
+0

謝謝,它完美的作品:)但有時應用程序崩潰,但也許這是因爲內存或有什麼錯誤? – user2900523