Question

I have a problem with my WebView. When I start the Application with Eclispe it works fine on my Device (Samsung Galaxy S2, Android 4.1.2/API 16). It loads the Website I've passed with webview.loadUrl('my url'); and also graps some javascript alerts and stuff. When I leave the application an restart it, it only works once more. When I restart the App second time it wont load any URL. I guess that it has something to do with savedInstanceState, but even if I set that Bundle to null at the very beginning of the Activities, it does not reload the URL.

protected void onCreate(Bundle savedInstanceState) {
    savedInstanceState=null;
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    browser = (WebView) findViewById(R.id.browser);
...
    browser.loadUrl(url);
}

I also tried to remove all Cookies with

    CookieSyncManager.createInstance(this);
    CookieManager cookieManager = CookieManager.getInstance();
    cookieManager.removeAllCookie();

Still having the same Bug. I allready read much solved posts and questions about WebView in Android, but nothing helped. Does anyboby have any idea? I am confused...

Whole onCreate Method:

@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
    savedInstanceState=null;
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    browser = (WebView) findViewById(R.id.browser);
    //      if (savedInstanceState != null) {
    //          browser.restoreState(savedInstanceState);
    //      }
    /* JavaScript must be enabled if you want it to work, obviously */
    browser.getSettings().setJavaScriptEnabled(true);

    final Context myApp = this;
    final Communicator c = new Communicator();

    /* WebChromeClient must be set BEFORE calling loadUrl! */
    browser.setWebChromeClient(new WebChromeClient() {
        @Override
        public boolean onJsAlert(WebView view, String url, String message,
                final android.webkit.JsResult result) {

            Log.d(TAG, "onJsAlert");

            try {
                JSONObject json = new JSONObject(message);
                if (json.has("func")) {
                    if (json.getString("func").equals("onUserInfos")) {
                        c.onUserInfos(json);
                        Log.d(TAG, "JSON verarbeitet");
                    }
                }

                return true;
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            // TODO
            Log.v(TAG, "JavaScript Alert empfangen");
            new AlertDialog.Builder(myApp)
                    .setTitle("javaScript dialog")
                    .setMessage(message)
                    .setPositiveButton(android.R.string.ok,
                            new AlertDialog.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int which) {
                                    result.confirm();
                                }
                            }).setCancelable(false).create().show();

            return false;
        };
    });
    // Reset all Cookies
    CookieSyncManager.createInstance(this);
    CookieManager cookieManager = CookieManager.getInstance();
    cookieManager.removeAllCookie();

    /* load a web page which uses the alert() function */
    String url = c.getGuiURL();
    browser.loadUrl(url);

}
Was it helpful?

Solution

When you leave your application the process probably keeps on running. So when you go back to your application the onResume method will be called instead of onCreate.

Checkout this page on the android developer site for more information about activity lifecycle http://developer.android.com/guide/components/activities.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top