The last link is not opening while returning from other tab and also onPause() is not working .it shows null pointer exception and app closes automatically whilie clicking other tab except first one . fragment tab code
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);
}
}
And Here are the 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);
}
}
Note that onSaveInstanceState() is called when your activity goes into the background and NOT when the app process is about to be killed.
onSaveInstanceState() is called every time the activity is destroyed and recreated, for example during an orientation change or when the device is running on low memory. Use it for saving state variables and taking appropriate action in case activity is killed and recreated.
Various Android system operations can affect the state of your fragment. To ensure the user's state is saved, the Android framework automatically saves and restores the fragments and the back stack. Therefore, you need to ensure that any data in your fragment is saved and restored as well.
The savedInstanceState is a reference to a Bundle object that is passed into the onCreate method of every Android Activity. Activities have the ability, under special circumstances, to restore themselves to a previous state using the data stored in this bundle.
Use This Library : https://github.com/frankiesardo/icepick
remove protected from WebView
@State
WebView myWebView; // This will be automatically saved and restored
Add Following Line In Your Activity Method Rest Will Be Handled.
In OnCreate Method
@Override public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Icepick.restoreInstanceState(this, savedInstanceState);
}
In onSaveInstanceState
@Override public void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
Icepick.saveInstanceState(this, outState);
}
Look like myWebView always null in your code:
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With