简体   繁体   English

Android WebView尚未完成

[英]Android WebView doesn't finish

I have a webview that I try to kill, but it is a hard bastard! 我有一个网络视图,我想杀死它,但这是一个混蛋! I've tried: 我试过了:

in my activity: 在我的活动中:

@Override
protected void onStop() {

    if (webView_ != null) {
        webView_.finish(getApplicationContext());
        webView_.destroy();
        webView_ = null;
    }
    super.onStop();
}

and in my webview: 在我的网络视图中:

public void finish(Context context) {

    stopLoading();
    pauseTimers();
    if (alarm_ != null)
        alarm_.cancelAlarm(context);
    if (webViewErrorHandler_ != null)
        webViewErrorHandler_.removeCallbacks(reloadRunnable_);
    if (requestQueue_ != null) {
        requestQueue_.clear();
        requestQueue_ = null;
    }
    callHiddenWebViewMethod("onPause");
}

private void callHiddenWebViewMethod(String name){
    if( this != null ){
        try {
            Method method = WebView.class.getMethod(name);
            method.invoke(this);
        } catch (NoSuchMethodException e) {
            Log.e("MYWEBVIEW", "No such method: " + name);
        } catch (IllegalAccessException e) {
            Log.e("MYWEBVIEW", "Illegal Access: " + name);
        } catch (InvocationTargetException e) {
            Log.e("MYWEBVIEW", "Invocation Target Exception: " + name);
        }
    }
}

but when I look at it in debugger there are still threads of this webview running! 但是当我在调试器中查看它时,仍在运行此webview的线程!

And you have to know that I especially want it to work after onPause ;) 而且您必须知道,我特别希望它在onPause之后工作;)

The activity's onStop is not guaranteed to be called. 不保证活动的onStop被调用。 Here is a portion of doc that reads: 这是文档的一部分,内容为:

The column labeled "Killable after?" 标有“之后可杀死?”的列 indicates whether or not the system can kill the process hosting the activity at any time after the method returns, without executing another line of the activity's code. 指示方法返回后,系统是否可以在不执行活动代码的另一行的情况下,随时终止提供活动的进程。 Three methods are marked "yes": (onPause(), onStop(), and onDestroy()). 三种方法标记为“是”:(onPause(),onStop()和onDestroy())。 Because onPause() is the first of the three, once the activity is created, onPause() is the last method that's guaranteed to be called before the process can be killed—if the system must recover memory in an emergency, then onStop() and onDestroy() might not be called. 因为onPause()是这三个函数中的第一个,所以一旦创建了活动,onPause()是保证在进程被杀死之前可以调用的最后一个方法-如果系统必须在紧急情况下恢复内存,则onStop()和onDestroy()可能不会被调用。 Therefore, you should use onPause() to write crucial persistent data (such as user edits) to storage. 因此,您应该使用onPause()将关键的持久性数据(例如用户编辑)写入存储。 However, you should be selective about what information must be retained during onPause(), because any blocking procedures in this method block the transition to the next activity and slow the user experience. 但是,您应该选择在onPause()期间必须保留哪些信息,因为此方法中的任何阻止过程都会阻止过渡到下一个活动,并减慢用户体验。

Check http://developer.android.com/guide/components/activities.html 检查http://developer.android.com/guide/components/activities.html

Try moving the code inside onStop to onPause method. 尝试将onStop内的代码移动到onPause方法。

Please post you findings. 请发表您的发现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM