4

I want to write a program that a ball move on screen, because of this, i read many tutorial and i cant understand this line:

mHolder = getHolder();
mHolder.addCallback(this);

what is "this" parameter in addCallback method? why i use this method? i read about SurfaceView and SurfaceHolder and Canvas but i cant good understand SurfaceHolder.what does it do?

I know maybe my question was stupid but this is a question ! Please explain about this concept.

Avadhani Y
  • 7,566
  • 19
  • 63
  • 90
Nice NICE
  • 111
  • 1
  • 3
  • 8

1 Answers1

5

"this" is a basic Java/OO concept ... here it refers to the object calling mHolder.addCallback(). In your class declaration you will see at the top that it "implements SurfaceHolder.Callback".

Since your class implements SurfaceHolder.Callback, it IS an instance of SurfaceHolder.Callback, and it can therefore pass a reference to itself ('this') to functions that take a SurfaceHolder.Callback parameter.

As for the "concept" of SurfaceHolder, it's merely the interface for app code to interact with a Surface. It's used in SurfaceView.java ... look for the private SurfaceHolder mSurfaceHolder declaration.

This is 100% speculation but I imagine SurfaceHolder was separated out this way because the designers envisaged having other kinds of SurfaceViews and wanted a standard way for app code to interact with them.

Reuben Scratton
  • 38,595
  • 9
  • 77
  • 86
  • Thanks for reply, but i cant understand getHoder() method. What does it do this method? – Nice NICE Apr 08 '13 at 15:26
  • 1
    SurfaceView.getHolder() returns the SurfaceHolder interface you use to interact with the Surface. I don't know how else or how better it can be explained than that. Maybe a close look at SurfaceView.java and SurfaceHolder.java will make it clearer. – Reuben Scratton Apr 08 '13 at 19:30