I am trying to reuse a webview across activity. Here's a sample of what I'm doing.
There are two activities, Main
and Secondary
.
In onResume() of Main i create a webview object, and I save it in my application context.
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");
Then on press of a button in Main
, I take him to Activity Secondary
. Here I get the already loaded webview from my Application
object and attach it to a ViewGroup
.
In onCreate()
of Secondary
-
WebView cachedWebView = ((WebViewReuseApplication) getApplicationContext()).getCachedWebView();
((LinearLayout) findViewById(R.id.webview_holder)).addView(cachedWebView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
I am doing all this because WebViews are very slow in Android and I really want it to be preloaded when it comes to Secondary
Activity.
While this works perfectly well, here's a problem. When it is in Secondary Activity, if I long press on a particular text to select, and click on Web Search, my app crashes. Is there any way to get around this? I really want this preloading thing and I for the same reason I cannot initialize WebView with activity context.
UPDATE -
Here's the logcat output i get -
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)