java - Why Does My Surface View Class Stops Rendering When Main Activity is Busy? -
i implementing simple render view class android extending surfaceview. main activity class includes infinite while loop other operations.
however, app shows black screen when main activity in infinite loop. far know, main activity , surface view classes have own separate threads, surface view class should keep rendering when main activity class busy. app works fine when prevent infinite loop setting 'running' boolean variable false in code.
what might reason of surface view class stops rendering when main activity in infinite loop?
my main activity , surface view classes follows:
import android.app.activity; import android.os.bundle; import android.view.window; import android.view.windowmanager; public class mainactivity extends activity{ rendersurfaceview renderview; public boolean running = true; @override protected void oncreate(bundle savedinstancestate) { super.oncreate ( savedinstancestate ); requestwindowfeature(window.feature_no_title); getwindow().setflags(windowmanager.layoutparams.flag_fullscreen, windowmanager.layoutparams.flag_fullscreen); renderview = new rendersurfaceview(this); setcontentview(renderview); loop(); } public void loop(){ running = true; while(running){ } } protected void onresume() { super.onresume(); renderview.resume(); } protected void onpause() { super.onpause(); renderview.pause(); } }
this render class extends surfaceview:
import java.util.random; import android.content.context; import android.graphics.canvas; import android.view.surfaceholder; import android.view.surfaceview; public class rendersurfaceview extends surfaceview implements runnable{ thread renderthread = null; surfaceholder holder; volatile boolean running = false; random r = new random(); public rendersurfaceview(context context) { super(context); holder = getholder(); } public void resume() { running = true; renderthread = new thread(this); renderthread.start(); } public void run() { while(running) { if(!holder.getsurface().isvalid()) continue; render(); } } private void render(){ canvas canvas = holder.lockcanvas(); canvas.drawrgb(r.nextint(255), r.nextint(255), r.nextint(255)); holder.unlockcanvasandpost(canvas); } public void pause() { running = false; while(true) { try { renderthread.join(); } catch (interruptedexception e) { // retry } } } }
this
renderview = new rendersurfaceview(this); setcontentview(renderview);
does not show view immediately. instead has go through layout traversal first, doesn't happen asynchronously activity
lifecycle methods, , happens on main thread. running infinite loop @ end of oncreate()
prevents layout traversal of view hierarchy, means surfaceview
never shown.
in theory experiment starting infinite loop delayed using handler
, experimental purposes maybe. in general infinite loops on main thread super bad idea , can't see reason why want this, apart experiments maybe.
my self-answer question modifying views in asynctask doinbackground() not (always) throw exception has bit more detail on what's going on behind scenes. question not directly related you're asking here, background same believe.
Comments
Post a Comment