-1

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.

enter image description here

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)
Sudarshan Bhat
  • 3,772
  • 2
  • 26
  • 53

1 Answers1

0

I think you need to add the below mentioned flag to your Intent where you call the WebView's Activity,

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

or at least this is what the logcat error report suggests,

Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. 

So I believe this is how you should do it for lower android versions.

If the above solution doesn't work, then replace all your applcationContext with your Activity context like this,

Instead of,

 WebView wv = new WebView(getApplicationContext());

try

WebView wv = new WebView(activity.this);

Not only here, but in all the places where you could have used getApplicationContext()

Andro Selva
  • 53,910
  • 52
  • 193
  • 240
  • If crash was happening when I'm starting the activity, then what you said might have been the problem. But this is happening when this system action bar's `Web search` option is trying to open another activity maybe browser. As far as I see it, they should have added this FLAG_ACTIVITY_NEW_TASK in their implementation of action bar. – Sudarshan Bhat Oct 11 '12 at 13:23
  • I have just provided the answer based on the exception from your logcat. did you give it a try? – Andro Selva Oct 11 '12 at 13:29
  • Yes I did. It doesn't help. I can give a sample project if required. – Sudarshan Bhat Oct 11 '12 at 13:39
  • It will work. But it will leak memory. It will be a huge leak and app will go out of memory in no time :( – Sudarshan Bhat Oct 11 '12 at 13:54
  • Then replace it only for the WebView or from where you call the intent or something. I don't see any other way and I have posted my known tactics. you are on your own then.. – Andro Selva Oct 11 '12 at 13:57