David
David

Reputation: 382

Android: Passing data from surfaceView to its parent activity

I am having trouble sending info from a surfaceView class to its parent class

the overlay activity sets its view as a drawing panel:

public class Overlay extends Activity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_Overlay);
       drawingPanel dPanel = new drawingPanel(this);
       setContentView(dPanel);

    }
}

then in the drawing panel:

public class drawingPanel extends SurfaceView implements SurfaceHolder.Callback {
    Context context = context;
    public Player(Context context, int num) throws ClassNotFoundException, IOException {
       super(context);
       this.context = context;
       SurfaceHolder holder = getHolder();
       holder.addCallback(this);
    }
    public boolean onTouchEvent(MotionEvent e){
       if(e.getAction()==MotionEvent.ACTION_DOWN){
           //send info to Overlay that drawingPanel was touched
       }
    }
} 

When surfaceView is touched, I want to send that info to Overlay. I can't simply use onTouchEvent in the Overlay activity because I need to draw stuff with drawingPanel. My main goal is to hide/show the action bar when the screen is touched while using a surfaceView. if there is another way to achieve that, please state so below.

Upvotes: 0

Views: 1102

Answers (1)

Dannie
Dannie

Reputation: 2470

Try adding this line to onCreate method of your Overlay class

   dPanel.setActivity(this);

And implementing setActivity() in the drawingPanel class like this:

public void setActivity(Activity overlayActivity) {
    mActivity = overlayActivity;
}

This way, you can use mActivity to reference your Activity and call public methods to "hide/show the action bar", like:

mActivity.getActionBar().hide();

and,

mActivity.getActionBar().show();

Upvotes: 1

Related Questions