1

I made an android app which works in webview. At my cellphone (android 6.0) works properly, but at cellphones with android 4.1.2 (lollipop) has some problems.

For example, there is a button on an index.html page. If the users press this button it redirects them to another html page. At this page the users can do some things (doesn't matter what) and then the app shows them a button. if they push this button, the app allows them to return to index.html. Before they return to index page, the webview saves something in localStorage variables, but the index.html reads them with the previous value and the action doesn't happen correctly.

Only if the user closes the app and then reopens it, it can read the new values in localStorage.

Can anybody help me with this problem?

Look my java code:

public class MainActivity extends AppCompatActivity {

private WebView myWebView;

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

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_main);

    myWebView = (WebView) this.findViewById(R.id.webView);

    myWebView.setWebChromeClient(new WebChromeClient() {
        @Override
        public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
            //Required functionality here
            return super.onJsAlert(view, url, message, result);
        }
    });

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

            if (url.contains("http") || url.contains("https")) { // Could be cleverer and use a regex
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                startActivity(intent);
                return true;
            }
            return false;

        //    view.loadUrl(url);
        //    return true;
        }
    });

    WebSettings settings = myWebView.getSettings();
    settings.setJavaScriptEnabled(true);
    settings.setDomStorageEnabled(true);
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
        settings.setDatabasePath("/data/data/" + myWebView.getContext().getPackageName() + "/databases/");
    }
    myWebView.setVerticalScrollBarEnabled(false);
    myWebView.setHorizontalScrollBarEnabled(false);
    myWebView.loadUrl("file:///android_asset/www/index.html");
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {

    String x = myWebView.getUrl();
    //x = x.replace("file:///android_asset/www/","");
    boolean ch_url = x.contains("index.html");

    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                if (myWebView.canGoBack()) {
                    if(ch_url == true){ 
                        finish();
                    }else {
                        myWebView.goBack();
                    }
                } else {
                    finish();
                }
                return true;
        }

    }
    return super.onKeyDown(keyCode, event);
}
}

Update 10/11/2016

This solution solved my problem:

if(Build.VERSION.SDK_INT >= 16) {
   setting.setAllowUniversalAccessFromFileURLs(true);
}

I found it here: Android 4.0 -> 4.3 (included) - Web storage lost between webview pages

Community
  • 1
  • 1

0 Answers0