Question

I have got a problem

When hitting the home button, my game thread class gets paused. But when re-entering the app and the onResume() method gets called, it should resume the game thread... everythin i get is a force close.

Here is my MainActivity:

public class MainActivity extends Activity {

private RelativeLayout relativeLayout;
private GameView gameView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    gameView = new GameView(this, this);
    setContentView(R.layout.activity_main);
    relativeLayout = (RelativeLayout)findViewById(R.id.mainView);
    relativeLayout.addView(gameView);
}

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

@Override
protected void onResume() {
    super.onResume();
    if(gameView.getGameThread() != null) {
        if(gameView.getGameThread().isRunning == false) {
            gameView.resume();
        }
    }
}
}

gameView is following class, a surfaceView getting rendered by GameThread:

public class GameView extends SurfaceView{

private GameThread gameThread;

public GameView(Context context, Activity activity) {
    super(context);

    gameThread = new GameThread(this);
    holder = getHolder();
    holder.addCallback(new SurfaceHolder.Callback() {

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {

        }

        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            gameThread.setRunning(true);
            gameThread.start();

        }

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width,
                int height) {

        }
    });
}

public GameThread getGameThread() {
    return gameThread;
}


public void update() {

}


public void pause() {
    gameThread.pause();
}

public void resume() {
    gameThread.resumeThread();
}
}

This is the GameThread class:

public class GameThread extends Thread{

private GameView view;
public boolean isRunning = false;

public GameThread(GameView view) {
    this.view = view;
}

public void setRunning(boolean setRunning) {
    isRunning = setRunning;
}

public void stopThread() {
    isRunning = false;
}

public void run() {
    while(isRunning) {
        Canvas c = null;
        view.update();
        try {
            c = view.getHolder().lockCanvas();
            synchronized (view.getHolder()) {
                view.draw(c);
            }
        }finally {
            if(c != null) {
                view.getHolder().unlockCanvasAndPost(c);
            }
        }
    }
}

public void startThread() {
    isRunning = true;
}

public void pause() {
    isRunning = false;
}

public void resumeThread() {
    isRunning = true;
}
}

I removed the irrelevant parts of the code.

When re-entering the app the android.view.Choreographer class opens

Any ideas?

Was it helpful?

Solution

Actually you're not pausing your thread (to be strict, there's no such thing as pausing a thread). You're ending your thread.

When you call pause() you set the isRunning flag to false, that makes the thread exit the loop and the thread ends. When you set your 'isRunning' to true again, the thread won't restart.

Actually you can't reuse threads in java, you have to create a new one, because once the thread loop ends, it's dead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top