简体   繁体   中英

SurfaceView, Black screen after reopening application

I have an activity which calls a class that extends a SurfaceView and implement Runnable and sets the contentView() of the activity class to the instance of the surfaceview class. After minimizing the activity, i pause and destroy the thread:

public void pause(){
    running = false;
    while(true){
        try{
            renderThread.join();
            break;
        }catch(InterruptedException e){
            //retry
        }
    }
}

when the activity resumes i recreate the thread:

 public void resume(){
    running = true;
    renderThread = new Thread(this);
    renderThread.start();
}

note that those are called within the onPause() and onResume() activity methods.

public void run(){
    while(running){//thred loop
            if(!holder.getSurface().isValid())
                continue;
                if(puzzleDrawn!=true)   {
                    canvas = holder.lockCanvas();
                    drawPuzzle(canvas);
                    holder.unlockCanvasAndPost(canvas);
                    }
    }
}
    public void drawPuzzle(canvas){
    //draws on canvas
    }

when i try to reopen the application i see a black screen. I need the drawPuzzle(canvas) method to be drawn just once. any tips?

if u need more info let me know! Thanks

In your pause method you are calling break in the wrong spot. What you should do is call break at the end of the while loop after the try and catch brackets and set your thread to null again outside the scope of the loop and just before you end your pause method, like this:

public void pause(){
 running = false;
 while(true){
    try{
        renderThread.join();
    }catch(InterruptedException e){
        //retry
    }
      break;
 }
 renderThread = null;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM