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

Shared Elements Between Fragments
Schedule a function to run at a future time with Cordova on Android
force toolbar to arrange children rtl android?
How can I post a parameter through JsonObjectRequest
Android how to open fragment from listview element
Android - How to add to ViewFlipper 2 different layouts s.t each has one YouTubePlayerView?
Parsing JSONObject to AlertDialog
google::protobuf used in android ndk develop
Is it good to add a listview inside Persistent bottom sheet in android?
Update Ionic 2 Platform
Writing data to BluetoothSocket on the UI thread
Android GridView is shows all images that are existing in a particular folder
No cached version of com.google.gms:google-services:1.x.x available for offline mode
View background not drawn correctly
Fatal - RuntimeException SQLite Remove Data from Database
How to validate virtual keyboard visibility?

Categories

HOME
twitter-bootstrap-3
turing-machines
cobalt
cheerio
jmeter
redux-form
seo
ado.net
missing-data
nfs
lanczos
windows-ce
mautic
libc
aws-sdk
inno-download-plugin
flask-sqlalchemy
ckfinder
pic32
tpm
ceylon
bioinformatics
google-cast
kudu
linkedhashmap
biztalk-rule-engine
reverse
jpad
health-monitoring
google-data-api
news
portforwarding
branching-and-merging
postgresql-8.4
subclipse
print-css
hammer.js
convex-optimization
x-ray
posixct
android-testing
filehandle
trigger.io
vibrate
sandcastle
webjars
microstation
adafruit
dotnetnuke-7
jquery-jscrollpane
xdoclet
lean
jclouds
tracker-enabled-dbcontext
spreadjs
np-spring
google-books
zscript
teamcity-9.1
git-ftp
nsurlsessiondatatask
shopizer
mmwormhole
java-security
cryptographichashfunction
infosphere-spl
theos
viewer
rickshaw
psr-4
paw
encryption-asymmetric
jqmodal
codeplex
functional-java
syndicationfeed
windows-embedded-standard
artemis
gtm-oauth2
windows-phone-8-sdk
dolby-audio-api
http-status-code-410
web-notifications
extreme-programming
midlet
web-farm
adobe-media-server
paginator
apache-commons-dbutils
expression-blend-4
workflow-services
unattended-processing
luajava
ajax-polling
web-garden
complex-event-processing
java1.4

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