Johan Hoeksma
Johan Hoeksma

Reputation: 3766

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?

Upvotes: 0

Views: 764

Answers (1)

Jitsu
Jitsu

Reputation: 769

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 :)

Upvotes: 1

Related Questions