我正在尝试跨活动重用 webview。这是我正在做的一个示例。
有两个活动,Main
和Secondary
。
在 Main 的 onResume() 中,我创建了一个 webview 对象,并将其保存在我的应用程序上下文中。
WebView wv = new WebView(getApplicationContext());
wv.getSettings().setJavaScriptEnabled(true);
wv.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
wv.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
Log.e("test", "onPageFinished");
((WebViewReuseApplication) getApplicationContext()).setCachedWebView(view);
}
});
wv.loadUrl("http://www.someurl.com/path/to/somewhere");
然后按一下按钮Main
,我带他去 Activity Secondary
。在这里,我从我的Application
对象中获取已经加载的 webview 并将其附加到ViewGroup
.
在- onCreate()
_Secondary
WebView cachedWebView = ((WebViewReuseApplication) getApplicationContext()).getCachedWebView();
((LinearLayout) findViewById(R.id.webview_holder)).addView(cachedWebView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
我这样做是因为 WebViews 在 Android 中非常慢,我真的希望它在Secondary
Activity 中被预加载。
虽然这工作得很好,但这里有一个问题。当它处于辅助活动中时,如果我长按特定文本进行选择,然后单击网页搜索,我的应用程序就会崩溃。有没有办法解决这个问题?我真的很想要这个预加载的东西,出于同样的原因,我不能用活动上下文初始化 WebView。
更新 -
这是我得到的 logcat 输出 -
10-11 18:41:09.318: E/AndroidRuntime(3359): FATAL EXCEPTION: main
10-11 18:41:09.318: E/AndroidRuntime(3359): android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
10-11 18:41:09.318: E/AndroidRuntime(3359): at android.app.ContextImpl.startActivity(ContextImpl.java:922)
10-11 18:41:09.318: E/AndroidRuntime(3359): at android.content.ContextWrapper.startActivity(ContextWrapper.java:283)
10-11 18:41:09.318: E/AndroidRuntime(3359): at android.webkit.SelectActionModeCallback.onActionItemClicked(SelectActionModeCallback.java:139)
10-11 18:41:09.318: E/AndroidRuntime(3359): at com.android.internal.policy.impl.PhoneWindow$DecorView$ActionModeCallbackWrapper.onActionItemClicked(PhoneWindow.java:2651)
10-11 18:41:09.318: E/AndroidRuntime(3359): at com.android.internal.app.ActionBarImpl$ActionModeImpl.onMenuItemSelected(ActionBarImpl.java:757)
10-11 18:41:09.318: E/AndroidRuntime(3359): at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
10-11 18:41:09.318: E/AndroidRuntime(3359): at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
10-11 18:41:09.318: E/AndroidRuntime(3359): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
10-11 18:41:09.318: E/AndroidRuntime(3359): at com.android.internal.view.menu.MenuPopupHelper.onItemClick(MenuPopupHelper.java:156)
10-11 18:41:09.318: E/AndroidRuntime(3359): at android.widget.AdapterView.performItemClick(AdapterView.java:292)
10-11 18:41:09.318: E/AndroidRuntime(3359): at android.widget.AbsListView.performItemClick(AbsListView.java:1182)
10-11 18:41:09.318: E/AndroidRuntime(3359): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2710)
10-11 18:41:09.318: E/AndroidRuntime(3359): at android.widget.AbsListView$1.run(AbsListView.java:3465)
10-11 18:41:09.318: E/AndroidRuntime(3359): at android.os.Handler.handleCallback(Handler.java:605)
10-11 18:41:09.318: E/AndroidRuntime(3359): at android.os.Handler.dispatchMessage(Handler.java:92)
10-11 18:41:09.318: E/AndroidRuntime(3359): at android.os.Looper.loop(Looper.java:137)
10-11 18:41:09.318: E/AndroidRuntime(3359): at android.app.ActivityThread.main(ActivityThread.java:4511)
10-11 18:41:09.318: E/AndroidRuntime(3359): at java.lang.reflect.Method.invokeNative(Native Method)
10-11 18:41:09.318: E/AndroidRuntime(3359): at java.lang.reflect.Method.invoke(Method.java:511)
10-11 18:41:09.318: E/AndroidRuntime(3359): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:986)
10-11 18:41:09.318: E/AndroidRuntime(3359): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:753)
10-11 18:41:09.318: E/AndroidRuntime(3359): at dalvik.system.NativeStart.main(Native Method)