onSaveInstanceState不起作用

最后一个链接没有打开,而从其他选项卡返回并onPause()不工作。它显示空指针exception和应用程序自动closures,除了第一个点击其他选项卡。 片段标签代码

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); } } 

Solutions Collecting From Web of "onSaveInstanceState不起作用"

使用这个库: https : //github.com/frankiesardo/icepick

从WebView中删除保护

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

在您的活动方法rest将被处理添加以下行。

在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); } 

看起来像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); } } 
 @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); }