3

All is ok, when I have started my application. But when I go to the next activity and come back the SurfaceView it looks like black. When I debug, I can see the lines that need to draw on the canvas running but nothing shows up.
Here is my code:

public class DrawView extends SurfaceView implements Runnable
{

    Thread thread = null;
    SurfaceHolder holder;
    Bitmap background;
    Canvas canvas;
    Matrix matrixUP = new Matrix();
    Matrix matrixDOWN = new Matrix();
    boolean ok = false;

    public DrawView(Context context) 
    {
        super(context);
        holder = getHolder();
    }

    public void run() 
    {
        while(ok)
        {
            if(!holder.getSurface().isValid())
            {
                continue;
            }
            watch_hand = Bitmap.createScaledBitmap(watch_hand, width/4, height/6, false);
            watch_hand_down = Bitmap.createScaledBitmap(watch_hand_down, width/4, height/6, false);
            background = BitmapFactory.decodeResource(getResources(), R.drawable.needles);
            background = Bitmap.createScaledBitmap(background, width, height/2+20, false);
            canvas = holder.lockCanvas();
            matrixUP.setTranslate(CENTER_X - watch_hand.getWidth()/2, height/4 - watch_hand.getHeight() - 40);
            matrixUP.preRotate((float) degreUP , watch_hand.getWidth()/2 , watch_hand.getHeight()-10);

            matrixDOWN.setTranslate(CENTER_X - watch_hand_down.getWidth()/2, height/2 - watch_hand_down.getHeight() - 20);
            matrixDOWN.preRotate((float) degreDOWN , watch_hand_down.getWidth()/2 , 10);

            canvas.drawARGB(255, 255, 255, 255);
            canvas.drawBitmap(background, 0, 0, null);
            canvas.drawBitmap(watch_hand, matrixUP, null);
            canvas.drawBitmap(watch_hand_down, matrixDOWN, null);
            holder.unlockCanvasAndPost(canvas);
        }           
    }

    public void pause()
    {
        ok = false;
        while(true)
        {
            try
            {
                thread.join();
            }catch (InterruptedException e)
            {
                e.printStackTrace();
            }
            break;
        }
        thread = null;
    }

    public void resume()
    {
        ok = true;
        thread = new Thread(this);
        thread.start();
    }           
}

as you see I'm using a resume and pause function. They are called from the onCreate. Why is it happening and how can I fix this?

Thanks!

Dhasneem
  • 4,037
  • 4
  • 33
  • 47
roiberg
  • 13,629
  • 12
  • 60
  • 91

2 Answers2

1

The answer depends on how you change to the other activity.

Basically, you have to think about how to store the current state of the surface when the activity pauses, stops or destroys.

for example you can overload onSaveInstanceState in your activity to save degreUP and degreDOWN (by the way they are not defined in the current code snippet). and restore them in onRestoreInstanceState. see Saving Android Activity state using Save Instance State

Community
  • 1
  • 1
onur güngör
  • 715
  • 5
  • 18
  • the degreUP is getting set "onclick". when i debug all is ok. the line canvas.drawARGB(255,255,255,255) is not rellated to anything and still its not showing. why is that? – roiberg Apr 23 '12 at 09:41
  • Like I said, the real problem is that you haven't implemented the means of resuming your application. – onur güngör Apr 23 '12 at 09:43
  • im sorry, but i am not sure you understand.. i dont care about shared preferances... i want the activity to start like its the first time. why would i need a shared preferance if i dont have anything to preferance. i just want the surfaceview to work from scratch. – roiberg Apr 23 '12 at 10:07
0

Call resume() and pause() methods of the surface view(in your case DrawView) from onResume() and onPause() methods of the activity in which the surface view is present. U mentioned that your were calling from onCreate().

This should solve the issue.

Chaitanya
  • 3,399
  • 6
  • 29
  • 47