So I have this code, I'm not the best at threads, but I thought I understood this much. For some reason this thread never leaves the wait() and resumes notifyAll() does nothing and doesn't bring back the view after the activity pauses/closes. I'm also kind of confused on what needs to be synchronized I see that people sometimes make generic Objects and sometimes people use other things. I've tried everything from making a generic object to using mSurfaceHolder and right now am using a Boolean object that runs the while loop. Any help greatly appreciated. **EDIT** To clarify the application doesn't crash when opened again after pausing/closing, but the view doesn't restore and is black. I've found a work around which includes running the thread constantly by making one while(true) loop and adding if(_run) and else if(!_run) but I know this isn't the most elegant solution and seems like the thread never closes. **EDIT**
- Code: Select all
class DrawThread extends Thread{
private SurfaceHolder mSurfaceHolder;
public DrawThread(SurfaceHolder surfaceHolder){
mSurfaceHolder = surfaceHolder;
}
public void onResume(){
synchronized(_run){
_run.notifyAll();
_run = true;
}
}
public void onPause(){
synchronized(_run){
_run = false;
}
}
public void setRunning(boolean run) {
synchronized(_run){
_run = run;
}
}
@Override
public void run() {
Canvas canvas = null;
while (_run){
System.out.println("RUNNING");
if(isDrawing == true){
System.out.println("IS DRAWING");
try{
canvas = mSurfaceHolder.lockCanvas(null);
if(mBitmap == null){
mBitmap = Bitmap.createBitmap (1, 1, Bitmap.Config.ARGB_8888);
}
final Canvas c = new Canvas (mBitmap);
c.drawColor(Color.WHITE);
canvas.drawColor(Color.WHITE);
c.drawBitmap(bBitmap, 0, 0,null);
//canvas.drawBitmap(bBitmap, 0, 0,null);
commandManager.executeAll(c,previewDoneHandler);
previewPath.draw(c);
canvas.drawBitmap (mBitmap, 0, 0,null);
} finally {
mSurfaceHolder.unlockCanvasAndPost(canvas);
}
}
}
synchronized(_run){
while(!_run){
try {
System.out.println("PAUSE");
_run.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}