I've got a basic app, that depending on the state either continuously
draws lines from the user's touch location (a basic paint app) or
loads a saved drawing.

In each one, when I Pause the activity (by loading the
PreferencesActivity or pressing Home Button) and then resume the
activity the canvas has been cleared.

I have an Activity that creates a surfaceView from XML, and the
surfaceView contains a thread which handles the drawing.

I do the pausing of the app in the onPause method of the Activity that
calls the SurfaceView, it sets mPaused to true which causes a pause in
the run thread. It is resumed from the surfaceCreated() method of the
surfaceView which sets mPaused false. The pause works fine, however as
said, when I resume in either a drawing or load state the canvas has
been cleared.

I also tested to see if the pausing/resuming was what was wiping the
canvas, so I tested a simple pause and resume without leaving the
activity and they worked fine. This shows that whatever is clearing
the screen must be due to what happens when the activity loses focus.
Do I need to save the state somehow as a bundle?

Here's my code for the Run method in the Drawing Thread.

 @Override
    public void run() {
        while (_run){
            // Set variable for running
            threadRunning=true;
            Canvas canvas = null;

            // This code pauses the thread
            synchronized (mPauseLock) {
                while (mPaused) {
                    try {
 
mPauseLock.wait();
                    } catch (InterruptedException e) {
                    }
                }
            }

            try{
                canvas = mSurfaceHolder.lockCanvas(null);

                // Split into DRAW and LOAD

                // DRAW IMAGE FROM PATHS
                if (STATE == STATE_DRAW){
                synchronized(mDrawingPaths) {
                    // If not finished drawing or resetthen draw onto
canvas
                    if (!finishDrawing&&resetCanvas==0){
                        doDraw(canvas);
                    }
                    }
                    if (resetCanvas>0){
                        System.out.println("Resetting canvas");
                    canvas.drawColor(Color.BLACK);
                    resetCanvas--;
                    }
                }

                // DRAW IMAGE FROM POINTS
                else if (STATE == STATE_LOAD){
                    doLoad(canvas, mMyLines);
                    // Then close the thread
                    setRunning(false);
                }

            }
             finally {
                if (canvas!=null){
                    mSurfaceHolder.unlockCanvasAndPost(canvas);
                }
            }
        } // While run
    }

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to