質問

(EDIT: Maybe I should rephrase the title, but it still describes the behavior correctly. Sometimes when you goBack() to the first URL it loads, and sometimes it doesn't. Even if I do it with loadURL() in onResume! See the code I appended to the Activity at the bottom of the question.)

The back button override works well and goes back through the pages I've visited. Until it should go back to the start URL which loads fine from onCreate. Then I get a small delay and a blank page, instead of the start page I saw before.

Is there some trick to it? Spamming some history array with the same URL more than once in onCreate?

The code is written with answers from this site. This is the entire java file.

package se.avia.tdca;

import se.avia.tdca.R;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
import android.view.KeyEvent;

public class Inspiration extends Activity {
    Sharedprefs share;
    WebView mywebview;

    @Override
    public void onBackPressed(){    //for < 4

        if (mywebview.canGoBack()) {
            mywebview.goBack();
            mywebview.reload(); //desperate try
            return;
        }
        super.onBackPressed();      
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && mywebview.canGoBack()) {
            mywebview.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        setContentView(R.layout.inspiration);

        share=new Sharedprefs(this);

        mywebview = (WebView) findViewById(R.id.inspiration_webview);

        String url = share.getSubUrl()+"/sources.html";

        mywebview.getSettings().setJavaScriptEnabled(true);
        mywebview.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);    //desperate try
        final Activity activity = this;
        mywebview.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress) {
                // Activities and WebViews measure progress with different
                // scales.
                // The progress meter will automatically disappear when we reach
                // 100%
                activity.setProgress(progress * 1000);
            }
        });
        mywebview.setWebViewClient(new WebViewClient() {
            public void onReceivedError(WebView view, int errorCode,
                    String description, String failingUrl) {
                Toast.makeText(activity, "Error loading Suppliers list: " + description,
                        Toast.LENGTH_SHORT).show();
            }
        });

        mywebview.loadUrl(url);

    }
}

Edit: I've now added this code to force load the start page when the activity is switched to, not just when it's created and when you go back. This ALSO sometimes doesn't load the start page.

@Override protected void onResume() { super.onResume(); Toast.makeText(getApplicationContext(), "onResume", Toast.LENGTH_SHORT).show(); mywebview.stopLoading(); String url = share.getSubUrl()+"/sources.html"; mywebview.loadUrl(url); }

The toast is shown every time I switch to this activity, but the start page only the first time. All the other times, the page is blank. Do I have to "save" mywebview somehow on onPause()? I just can't see anything wrong with the above code.

役に立ちましたか?

解決

After rebuilding the activity from the example in the docs, I still got the problem. And also, sometimes it would stop loading after a few lines of html and sometimes after half a screenful.

A single line in the valid CSS for the start page caused the rendering to break without any error message. The page worked fine in Chrome on the same phone, which points to the implementation of WebViews being incomplete in comparison.

Thanks to people answering and sorry for the time wasted. It's just too bad to follow the standards, program correctly, and have bugs trip you up and make a project negatively profitable.

If the Google devs want to patch this or at least trigger an error, the offending line was span:empty {display:none;}

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top