1

I am testing surfaceView, basically the app jsut changes the color of the background.

The app starts with no issue but when I "pause" the activity and "resume" it, the app is blocked in the "pause()" method loop.

this is my code:

import java.util.Random;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.util.Log;
import android.view.Menu;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class SurfaceViewTest extends Activity {
    FastRenderView renderView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_surface_view_test);
        renderView= new FastRenderView(this);
        setContentView(renderView);
    }

    protected void onResume() {
        super.onResume();
        renderView.resume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        renderView.pause();
    }

    public class FastRenderView extends SurfaceView implements Runnable {
        Thread renderThread=null;
        SurfaceHolder holder;
        volatile boolean running=false;

        Random rand= new Random();                      //random number creator----

        public FastRenderView(Context context) {
            super(context);
            holder=getHolder();         //<<-check the this statement
        }
        public void resume() {
            Log.d("ZR", "in resume");
            running=true;
            renderThread=new Thread(this);
            renderThread.start();   //<<<--AVVIA IL THREAD
        }
        @Override
        public void run() {
            Log.d("ZR", "in running");
            while(running){
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                if(!holder.getSurface().isValid())
                    continue;
                Canvas canvas = holder.lockCanvas();
                canvas.drawRGB(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255));
                holder.unlockCanvasAndPost(canvas);
            }
        }
        public void pause() {
            running = false;
            Log.d("ZR", "in pause");
            while(true){ //<<<<<<<<<<<<<<CHECKKKKKK
                try {
                    renderThread.join();
                    Log.d("ZR", "in pause end");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
Carlo Luther
  • 2,402
  • 7
  • 46
  • 75

1 Answers1

2

The while(true) loop without a break in it will never stop in method pause(). I would expect something like a return after succesfully joined the rendering thread (waited till it ended)

With your volatile declaration of running the while-loop in run() should be ok.

jboi
  • 11,324
  • 4
  • 36
  • 43
  • Using "break" it works, can't understand why in these examples nobody uses the break? ---> http://stackoverflow.com/questions/3527621/how-to-pause-and-resume-a-surfaceview-thread?rq=1 and http://stackoverflow.com/questions/14013468/pausing-threads-properly-onpause-and-onresume-with-a-surfaceview-and-thread?rq=1 – Carlo Luther Oct 05 '13 at 21:06
  • Good question. Have you checked if one of them has asked another question like yours later :-) – jboi Oct 05 '13 at 21:24