0

I'm working in a project in which we need to load an Activity with a WebView, we need to load some HTML with Angular but we need to have this activity in the background until this activity/webview with angular has loaded finally.

Is there any way to do this with an activity?

Víctor Martín
  • 3,352
  • 7
  • 48
  • 94

2 Answers2

1

When an Activity goes to background, it is not running anymore. You can do background tasks with Service, but not Activity. You can work on onResume() method of your Activity with the webview when angular has loaded your required information.

shizhen
  • 12,251
  • 9
  • 52
  • 88
0

WebView is most of the time inside an Activity. So the Activity, where the WebView is, will always be there until you kill it. Unless you really want the Activity to be really running on the background, then use a Service instead.

Because you might need to use a Service for what you want, then I guess you need to use a BroadcastReceiver inside the Service. Inside the BroadcastReceiver, you have a custom tag that when called using a sendBroadcast() will startActivity() your webview/activity. I don't guarantee the speed of the loading for the WebView because you mentioned that you want to preload the page but that'll do it. To lessen the load time for the WebView, you must enable caching on the WebView.

Another problem that I'm thinking that would happen if you really want to hold a WebView on the background (if you can even do that) is memory leak. Because you want to keep the WebView alive at all times even if it isn't even shown on the screen, you'll probably don't want it to be garbage collected which will be a really bad problem on android devices with low memory. You won't get it often on devices with lots of memory but not of 300mb devices.

rmanalo
  • 342
  • 3
  • 23
  • How I can use a Service to load the WebView?? How I can show the webview in anywhere of the app? – Víctor Martín Jan 22 '19 at 09:55
  • Oh, so that's what you want to do. Can you share why you need to show the webview anywhere on the app? Is it like an advertisement? It'll help me think of some other way we could accomplish what you want to do. – rmanalo Jan 22 '19 at 09:57
  • Yeah, I need a webview ready all the time for the execution of some angular methods at anytime in any activity/fragment – Víctor Martín Jan 22 '19 at 10:33
  • What do you need to do with the results of the method? – rmanalo Jan 23 '19 at 01:59