Search code examples
androidsurfaceviewglsurfaceview

Menu to launch GLSurfaceViews


I wrote a cellphone interface for a measurement device. I have a bunch of distinct functional screens in the from of GLSurfaceViews. I can launch the renders correctly, but I can't figure out how to make the back button work. My render code looks like:

Button mSpecButton = new Button(this);
mSpecButton.setText("Spectrometer");
mSpecButton.setOnClickListener(new View.OnClickListener()
{
    public void onClick(View v)
    {
        glSurfaceView = new GLSurfaceView(mRun);
        mRender = new renderSpectro(mRun);
        glSurfaceView.setRenderer(mRender);
        setContentView(glSurfaceView);
    }
});

I can't figure out where to put a public boolean onKeyDown(int keyCode, KeyEvent event)


Solution

  • The onKeyDown() method must be placed in the activity. if you want to use it to control the GLSurfaceView or Renderer you must use some other way to send it there. For example in your main activity you can have:

    //Somewhere in the code
    GLSurfaceView glSurfaceView = new myGLSurfaceView();
    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        glSurfaceView.doBackAction(); //Or something similar
    }
    

    And in your myGLSurfaceView you just have a method:

    public void doBackAction() {
        //Yay
    }
    

    If you are concerned about thread safety it gets a little more complicated but the initial onKeyDown() must be in the activity.