当webview具有webviewclient时,活动会被破坏

时间:2012-11-19 20:25:50

标签: android android-activity webview webviewclient

这是我的代码:

public class AuthActivity extends Activity {
public static final String REDIRECT_URL = "redirectUrl";
WebView webview;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    webview = new WebView(this);
    setContentView(webview);

    webview.setWebViewClient(new WebViewClient() {

        @Override
        public void onPageFinished(WebView view, String url) {
            parseUrl(url);
            super.onPageFinished(view, url);
        }
        @Override
        public void onReceivedError(WebView view, int errorCode,
                String description, String failingUrl) {
            Toast.makeText(getApplicationContext(), description, Toast.LENGTH_LONG).show();
            super.onReceivedError(view, errorCode, description, failingUrl);
        }
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return true;
        }
    });
    webview.clearCache(true);
    webview.clearHistory();
    webview.getSettings().setJavaScriptEnabled(true);
    webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    CookieSyncManager.createInstance(this);
    CookieManager cookieManager = CookieManager.getInstance();
    cookieManager.removeAllCookie();

    String url = VKPoster.getAuthUrl( );
    webview.loadUrl(url);
}

private void parseUrl(String url) {
    try {
        if(url==null)
            return;
        if(!url.contains("error=")){
            Intent intent=new Intent();
            intent.putExtra(REDIRECT_URL, url);
            setResult(Activity.RESULT_OK, intent);
        }
        finish();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
}

当活动开始加载没有webview客户端的页面时,它会停止运行,但是当设置了webviewclient时,它可以正常工作。

有人可以向我解释这种行为吗?

1 个答案:

答案 0 :(得分:0)

问题在于此代码(当我发布问题时,我已经看到了。这绝对是吹嘘)

private void parseUrl(String url) {
try {
    if(url==null)
        return;
    if(!url.contains("error=")){
        Intent intent=new Intent();
        intent.putExtra(REDIRECT_URL, url);
        setResult(Activity.RESULT_OK, intent);
    }
    /* I finished app every time page loaded! What a shame */
    finish();
} catch (Exception e) {
    e.printStackTrace();
}

}