Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onSaveInstanceState is not working

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);
    }
}
like image 808
Anirban Bhui Avatar asked Mar 28 '16 15:03

Anirban Bhui


People also ask

When onSaveInstanceState () is called?

Note that onSaveInstanceState() is called when your activity goes into the background and NOT when the app process is about to be killed.

What is the purpose of using onSaveInstanceState () here?

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.

How to Save state of a fragment?

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.

What is savedInstanceState in Android?

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.


2 Answers

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);
   }
like image 124
Praveen Avatar answered Oct 05 '22 18:10

Praveen


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);
}
} 
like image 45
Khang .NT Avatar answered Oct 05 '22 19:10

Khang .NT