android


Black webview onResume


I'm having a WebView in my app, and I want it running forever.
When I return to my app (onResume), the WebView is a black screen...
I tried multiple solutions, and Layouts. I am adding the WebView with the function setContentView(w);
On the first run it works fine. When I push the home button the WebView is still running (console.log(), from the javascript give feedback). Comming back to the app, I see a black screen. The WebView is still running, but I cant see it :(.
I've tried also to add it to an .xml View file, but without result. I want also to prevent the page from reloading. I tried to set the View visible. Tried some functions as w.invalidate().
MainActivity.java
public class MainActivity extends Activity {
public static final String EXTRA_FROM_NOTIFICATION = "EXTRA_FROM_NOTIFICATION";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.v("main"," create ");
if (savedInstanceState == null) {
Intent i = new Intent(MainActivity.this, TestService.class);
TestService.setMain(this);
TestService.setView();
MainActivity.this.startService(i);
} else {
TestService.setAgain();
}
}
#Override
public void onBackPressed() {
Log.v("main","back press");
}
#Override
public void onPause() {
super.onPause();
}
#Override
public void onResume() {
super.onResume();
}
}
TestService.java
public class TestService extends Service {
private static WebView w;
private static MainActivity ma;
#Override
public IBinder onBind(Intent arg0) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
}
public static void setMain(MainActivity a) {
ma = a;
Log.e("main", " setData " );
}
public static void setView() {
Log.e("TestService", "FIRST TIME SETUP");
w = new WebView(ma);
ma.setContentView(w);
w.getSettings().setDomStorageEnabled(true); // use localstorage
w.getSettings().setJavaScriptEnabled(true);
w.loadUrl("file:///android_asset/www/index.html");
}
public static void setAgain() {
w.bringToFront();
w.loadUrl("javascript: test();");
Log.e("TestService", w.getVisibility() + ""); // returns 0 (View.VISIBLE)
}
}
index.html
<html>
<head>
<title>Webview test Android</title>
<style>
body {
background: white;
}
html {
min-width:100%;
min-height:100%;
}
</style>
</head>
<body>
Counter<br>
<h1><div id="counter"></div></h1>
<script>
var counter = 0;
document.getElementById("counter").innerHTML = counter;
setTimeout(function(){ count(); }, 1000);
function test() {
console.log("Test");
alert("Jooo");
}
function count() {
counter++;
document.getElementById("counter").innerHTML = counter;
setTimeout(function(){ count(); }, 1000);
console.log("count " + counter);
}
</script>
<font color="#ff0">Test</font>
</body>
</html>
Maybe anyone knows the right way to go?
Nope.
Service is explicitly meant for processing data in background.
Displaying anything from a service is not allowed (except for a few special cases), nor recommended.
In your case i can see you're doing
setContentView(...);
only once. Try to re-attach it in the
else {
TestService.setAgain();
}
condition, because that is why you're getting black screen.
However, still I can see a very bad design here. I think I could help, if you could precisely state what you're trying to achieve - ie. what is the purpose for your workflow.
I guess it may be related to some kind of javascript running in the background? Generally this is a bad practice. User will expect the application to stop consuming resources one home is pressed or app is taken off the foreground. Of course, unless you're writing javascript based bitcoin miner :)

Related Links

Error when exporting app that used Proguard
Download images and save it
Listview and activity
Parsing runtime errors: saying i have null reference: Android Studio
ModernAsyncTask IllegalStateException
Is it possible to use the “Hamburger icon” animation from Android in another button?
Android Image decoding from base64 to bitmap returns null . while its is working fine in my other test app .
notifyDataSetChanged on CustomAdapter not Working
how to stop an android START.STICKY service after application has been killed and started again
javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException Android
Libgdx text in place rotation
Robotium case-insensitive search
How to get the count of Listview item selection in Android
Is it possible to specify an activity to be created when the OS resumes an app it has previously killed?
Exception when calling a post url from retrofit android
Android studio 1.4 Keystore was tampered with, or password was incorrect attempt create a new keystore result in the same error message

Categories

HOME
api
c++11
nvd3.js
obfuscation
oracle-apex-5
solaris
graphics
spring-data-redis
xbee
vxml
odoo-8
openstreetmap
jxl
symfony-console
jmp
breadth-first-search
rubymotion
angular-routing
pchart
exploit
jboss6.x
xcode-ui-testing
nstimer
errbot
ntp
postback
powershell-ise
sql-server-2000
testbed
erb
spring-security4
jett
keras-layer
getpixel
pop3
navigator
engine.io
playframework-2.1
xcode7.3
oracle-service-bus
monkey-testing
httr
openrasta
bluez
payara-micro
truezip
filehandle
arules
renaming
cudd
vulcanize
nikeplus-api
rspec2
appscale
google-query-language
cda
cycle2
dynatree
lazybones
rate-limiting
xvim
ember-cli-rails
haskell-warp
fail2ban
jquery-forms-plugin
zend-currency
shopizer
document-repository
mruby
teaspoon
formview
appserver
grails-domain-class
live555
twitter-bootstrap-rails
koala
ie8-compatibility-mode
scrypt
modular
cocoalibspotify-2.0
printdialog
onmousemove
fusefabric
pinch
fsevents
mysql-error-1205
cgbitmapcontextcreate
collectionviewsource
nhprof
commerceserver2007
updatesourcetrigger
globals
rowlex
interprocess
message-passing
nstokenfield
murmurhash
visualj#
linfu-dynamicproxy
sqlobject
appliance

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App