0

The features are enabled once the onCreate() methood is executed for the first time, but after i do a screen rotation none of the features get saved, even though I've overridden the onSaveInstanceState() method and I've also tried using the onRestoreInstanceState() method. Kindly help me out.

I'll attach the code below. Activity file

import android.app.LoaderManager;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.Loader;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.webkit.CookieManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class Main3Activity extends AppCompatActivity implements LoaderManager.LoaderCallbacks{

    Toolbar toolbar;
    WebView webView;
    ProgressDialog progressDialog;
    final String urld="https://www.google.co.in/";
    final int LOADER_ID=1;
    Bundle bundle;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setDisplayShowHomeEnabled(true);

        }
        webView = (WebView)findViewById(R.id.wb1);

        progressDialog = new ProgressDialog(Main3Activity.this);
        progressDialog.setTitle("Loading");
        progressDialog.setIndeterminate(true);
        progressDialog.setMessage("Please Wait...");
        progressDialog.setCancelable(true);
        if(savedInstanceState!=null)
        {
            webView.restoreState(savedInstanceState);
            webView.getSettings().setJavaScriptEnabled(true);
        }
        else {
            webView.getSettings().setJavaScriptEnabled(true);
            webView.setFocusable(true);
            webView.setFocusableInTouchMode(true);
            //webView.getSettings().setSupportZoom(true);
            webView.getSettings().setBuiltInZoomControls(true);
            webView.getSettings().setDisplayZoomControls(true);
            webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
            //webView.setInitialScale(1);
            webView.getSettings().setLoadWithOverviewMode(true);
            webView.getSettings().setUseWideViewPort(true);
            webView.setBackgroundColor(Color.WHITE);
            //webView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
            webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
            webView.getSettings().setDomStorageEnabled(true);
            //webView.getSettings().setDatabaseEnabled(true);
            webView.getSettings().setAppCacheEnabled(true);
            //webView.setWebChromeClient(new WebChromeClient());
            if (Build.VERSION.SDK_INT >= 19) {
                webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
            } else {
                webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
            }

            webView.setWebViewClient(new WebViewClient() {
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {

                    /*if( URLUtil.isNetworkUrl(url) ) {
                        return false;
                    }
                    if (appInstalledOrNot(url)) {
                        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                        startActivity( intent );
                    } else {
                        // do something if app is not installed
                    }
                    return true;*/
                    if (Uri.parse(url).getHost().endsWith("google.co.in")) {
                        return false;
                    }

                    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                    view.getContext().startActivity(intent);
                    CookieManager.getInstance().setAcceptCookie(true);
                    return true;

                }

                @Override
                public void onPageStarted(WebView view, String url, Bitmap favicon) {
                    progressDialog.show();
                    super.onPageStarted(view, url, favicon);
                }

                @Override
                public void onPageFinished(WebView view, String url) {
                    progressDialog.dismiss();
                    super.onPageFinished(view, url);
                }
            });
        }

            bundle = savedInstanceState;

            LoaderManager loaderManager = getLoaderManager();

            // Initialize the loader. Pass in the int ID constant defined above and pass in null for
            // the bundle. Pass in this activity for the LoaderCallbacks parameter (which is valid
            // because this activity implements the LoaderCallbacks interface).
            loaderManager.initLoader(LOADER_ID, null, this);
    }
    /*private boolean appInstalledOrNot(String uri) {
        PackageManager pm = getPackageManager();
        try {
            pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
            return true;
        } catch (PackageManager.NameNotFoundException e) {
        }

        return false;
    }*/

    @Override
    public void onBackPressed() {
        if(webView.canGoBack())
        {
            webView.goBack();
        }
        else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // handle arrow click here
        if (item.getItemId() == android.R.id.home) {
            finish(); // close this activity and return to preview activity (if there is any)
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public Loader onCreateLoader(int i, Bundle bundle) {

        return new LoadingPageInBackground(this,urld,bundle,webView,progressDialog);
    }

    @Override
    public void onLoadFinished(Loader loader, Object o) {

    }

    @Override
    public void onLoaderReset(Loader loader) {
        //webView.setVisibility(WebView.GONE);
    }


    @Override
    protected void onSaveInstanceState(Bundle outState) {
        webView.saveState(outState);
        Toast.makeText(Main3Activity.this,"Saved",Toast.LENGTH_SHORT).show();
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onRestoreInstanceState(Bundle state) {
        webView.restoreState(state);
        Toast.makeText(Main3Activity.this,"Restored",Toast.LENGTH_SHORT).show();
        super.onRestoreInstanceState(state);
    }
}
CLay
  • 19
  • 6

1 Answers1

0

restoreState is not reliable, based on the documentation for restoreState :

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.

Based on the documentation for saveState:

Please note that this method no longer stores the display data for this WebView

You have to reload your WebView in onCreate(). You can store the URL in SharedPreference and load it in onCreate().

Another alternative is to handle the Orientation by your self and keep WebView unchanged. You can handle the orientation change by adding android:configChanges="orientation" in your manifest and Overriding onConfigurationChanged().

Sagar
  • 23,903
  • 4
  • 62
  • 62
  • Thanks for your suggestion Sagar,but i've tried the website on chrome or any browser, I've changed the orientation but the webpage didn't have its settings changed, what do you think is the reason? – CLay May 31 '18 at 04:20
  • @CLay as I said restore is not reliable, set the setting again in onCreate – Sagar May 31 '18 at 04:33
  • Like you mean again call all the methods like setBuiltInZoomControls and all? it would work(i tried it with setJavaScriptEnabled and when i rotated my phone it worked),but i wanted to know what was the cause for the attributes not being saved in my WebView and working on other browsers. Also thanks for letting me know that restoreState isn't reliable, ill remove it from my code. – CLay May 31 '18 at 08:33
  • @CLay other browsers could be handling it in same way. It's just not noticable. Check this [SO](https://stackoverflow.com/questions/18479519/how-to-save-restore-webview-state) for more ideas – Sagar May 31 '18 at 08:36
  • thanks for the info @Sagar, if you find any info on that please do share – CLay Jun 01 '18 at 16:36
  • @CLayc sure. for the context of the question if answer was useful then remember to mark it as accepted. – Sagar Jun 02 '18 at 01:57