2016-03-28 42 views
8

最後一個鏈接沒有打開,而從其他標籤返回 並且onPause()不起作用。它顯示空指針異常,並且應用程序自動關閉除第一個標籤之外的其他標籤。 片段標籤代碼onSaveInstanceState不起作用

public class FragmentTab extends Fragment { 
    protected WebView myWebView; 
    @Override 
    public void onCreate(final Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
    } 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View v = inflater.inflate(R.layout.fragment_layout, container, false); 
     WebView myWebView = (WebView) v.findViewById(R.id.webview); 
     WebSettings webSettings = myWebView.getSettings(); 
     webSettings.setJavaScriptEnabled(true); 
     myWebView.setWebViewClient(new WebViewClient() { 
      public boolean shouldOverrideUrlLoading(WebView view, String url) { 
       view.loadUrl(url); 
       return false; 
      } 
     }); 
     if(savedInstanceState != null) 
      myWebView.restoreState(savedInstanceState); 
     else 
      myWebView.loadUrl("http://www.example.com"); 
     return v; 
    } 
    @Override 
public void onSaveInstanceState(Bundle outState) { 
     super.onSaveInstanceState(outState); 
     myWebView.saveState(outState); 
    } 
} 

,這裏是在MainActivity

public class MainActivity extends FragmentActivity { 
    private FragmentTabHost mTabHost; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.activity_main); 
     mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost); 
     mTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent); 
      mTabHost.addTab(
        mTabHost.newTabSpec("tab1").setIndicator(null,ContextCompat.getDrawable(this,R.drawable.home)), 
        FragmentTab.class, null); 
      mTabHost.addTab(
        mTabHost.newTabSpec("tab2").setIndicator(null,ContextCompat.getDrawable(this,R.drawable.deals_offers)), 
        FragmentTab.class, null); 
      mTabHost.addTab(
        mTabHost.newTabSpec("tab3").setIndicator(null,ContextCompat.getDrawable(this,R.drawable.profile)), 
        FragmentTab.class, null); 
      mTabHost.addTab(
        mTabHost.newTabSpec("tab4").setIndicator(null, ContextCompat.getDrawable(this, R.drawable.menu)), 
        FragmentTab.class, null); 
    } 
} 
+0

我總是用setRetainInstance(真);爲了避免在配置更改後重新創建片段 –

+0

無變化!在使用setRetainInstance(true)後; –

+0

將myWebview和clientWebView定義爲片段頂部的靜態字段並使用保留狀態 –

回答

2

樣子myWebView總是在你的代碼無效:

public class FragmentTab extends Fragment { 
protected WebView myWebView; 
@Override 
public void onCreate(final Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
} 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    View v = inflater.inflate(R.layout.fragment_layout, container, false); 
    myWebView = (WebView) v.findViewById(R.id.webview); 

    // instead of WebView myWebView = (WebView) v.findViewById(R.id.webview); 

    WebSettings webSettings = myWebView.getSettings(); 
    webSettings.setJavaScriptEnabled(true); 
    myWebView.setWebViewClient(new WebViewClient() { 
     public boolean shouldOverrideUrlLoading(WebView view, String url) { 
      view.loadUrl(url); 
      return false; 
     } 
    }); 
    if(savedInstanceState != null) 
     myWebView.restoreState(savedInstanceState); 
    else 
     myWebView.loadUrl("http://www.example.com"); 
    return v; 
} 
@Override 
public void onSaveInstanceState(Bundle outState) { 
    super.onSaveInstanceState(outState); 
    myWebView.saveState(outState); 
} 
} 
+0

和以前一樣!它不保存最後的狀態 –

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

@Override 
public void onRestoreInstanceState(Bundle outState) { 
    super.onRestoreInstanceState(outState); 
    myWebView.restoreState(outState); 
} 

根據源代碼。

/** 
    * Restores the state of this WebView from the given Bundle. This method is 
    * intended for use in {@link android.app.Activity#onRestoreInstanceState} 
    * and should be called to restore the state of this WebView. If 
    * it is called after this WebView has had a chance to build state (load 
    * pages, create a back/forward list, etc.) there may be undesirable 
    * side-effects. Please note that this method no longer restores the 
    * display data for this WebView. 
    * 
    * @param inState the incoming Bundle of state 
    * @return the restored back/forward list or null if restoreState failed 
    */ 
    public WebBackForwardList restoreState(Bundle inState) { 
     checkThread(); 
     return mProvider.restoreState(inState); 
    } 
3

使用這個庫:https://github.com/frankiesardo/icepick

remove從的WebView

@State 
WebView myWebView; // This will be automatically saved and restored 

添加保護下面一行在你的鍛鍊方式其餘部分將被處理。

在onCreate方法

@Override public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     Icepick.restoreInstanceState(this, savedInstanceState); 
    } 

的onSaveInstanceState在

@Override public void onSaveInstanceState(Bundle outState) 
    { 
     super.onSaveInstanceState(outState); 
     Icepick.saveInstanceState(this, outState); 
    }