title: libGDX 1.4.1 button listener on a secondary screen
body: <p>I have two screens, a <code>MainActivity</code> in my core folder, that has the <code>render()</code> method split into a few <code>switch</code> cases. On game-over, this part of the <code>switch</code> case gets triggered, which calls the render part of my game-over class: </p>
  
  <pre><code>         case STOPPED:
  
                  // Exit and clean the game
  
                  gameOverScreen.render(Gdx.graphics.getDeltaTime());
  
  
                  break;
                  ...
  </code></pre>
  
  <p>This is my GameOverScreen class:</p>
  
  <pre><code>public class GameOverScreen implements Screen {
  
      private SpriteBatch gameOverBatch;
      private FreeTypeFontGenerator gameOverFontGen;
      private FreeTypeFontGenerator.FreeTypeFontParameter gameOverLogoParam;
      private FreeTypeFontGenerator.FreeTypeFontParameter gameOverButtonParam;
      private final MainActivity mainActivity;
      private OrthographicCamera camera;
      private BitmapFont bitmapLogoFont;
      private BitmapFont bitmapButtonFont;
      private Button exitGameButton;
      private Button gameRestartButton;
      private Sound buttonSound;
      private Stage stage;
      private float w;
      private float h;
  
      // Constructor
      public GameOverScreen(MainActivity mainActivity) {
  
          w = Gdx.graphics.getWidth();
          h = Gdx.graphics.getHeight();
  
          gameOverBatch = new SpriteBatch();
  
          this.mainActivity = mainActivity;
          camera = new OrthographicCamera();
          camera.setToOrtho(false, 300, 300);
  
          // Instantiate the font for this screen from file
          gameOverFontGen = new FreeTypeFontGenerator(Gdx.files.internal("fonts/SF_Wonder_Comic.ttf"));
          gameOverLogoParam = new FreeTypeFontGenerator.FreeTypeFontParameter();
          gameOverLogoParam.size = 110;
  
          gameOverButtonParam = new FreeTypeFontGenerator.FreeTypeFontParameter();
          gameOverButtonParam.size = 40;
  
          // Font for the logo
          bitmapLogoFont = new BitmapFont();
          bitmapLogoFont = gameOverFontGen.generateFont(gameOverLogoParam);
  
  
          // Font for the buttons
          bitmapButtonFont = new BitmapFont();
          bitmapButtonFont = gameOverFontGen.generateFont(gameOverButtonParam);
  
          // Instantiate the buttonSound
          buttonSound = Gdx.audio.newSound(Gdx.files.internal("sounds/pauseBtn_sound.ogg"));
  
  /*************************************** Create a Stage *******************************************/
          stage = new Stage();
  
  
          // Add some actors as the buttons
          exitGameButton = new Button(new TextureRegionDrawable(
                  new TextureRegion(new Texture(Gdx.files.internal("images/off_red.png")))),
                  new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("images/off_white.png")))));
          //exitGameButton.setX((w, 150));
          //pauseButton.setY(flipCoordinates(h, 150));
  
          exitGameButton.setOrigin(exitGameButton.getWidth() / 2, exitGameButton.getHeight() / 2);
          exitGameButton.setBounds(w / 2 + 100, h / 2 - 60, exitGameButton.getWidth(), exitGameButton.getHeight());
  
          exitGameButton.act(Gdx.graphics.getDeltaTime());
  
          stage.addActor(exitGameButton);
  
          exitGameButton.addListener(new ChangeListener() {
              @Override
              public void changed (ChangeEvent event, Actor actor) {
  
                  buttonSound.play();
              }
          });
  
          // Add some actors as the buttons
          TextButton.TextButtonStyle restartStyle = new TextButton.TextButtonStyle();
          restartStyle.font = bitmapButtonFont;
          restartStyle.up = new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("images/back_red.png"))));
          restartStyle.down = new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("images/back_white.png"))));
          gameRestartButton = new Button(new TextButton.TextButtonStyle(restartStyle));
  
          gameRestartButton.setOrigin(gameRestartButton.getWidth() / 2, gameRestartButton.getHeight() / 2);
          gameRestartButton.setBounds(w / 2 - 100, h / 2 - 60, gameRestartButton.getWidth(), gameRestartButton.getHeight());
  
          gameRestartButton.act(Gdx.graphics.getDeltaTime());
          stage.addActor(gameRestartButton);
  
          // Capture the event listener for the return button
          gameRestartButton.addListener(new ChangeListener() {
              @Override
              public void changed(ChangeEvent event, Actor actor) {
  
                  // Do something on restart
                  //MainActivity.state = MainActivity.State.RUN;
                  Gdx.app.log("GameRestartButton", " has been pressed");
              }
          });
  
          gameRestartButton.act(Gdx.graphics.getDeltaTime());
  
          //stage.addActor(exitGameButton);
          //stage.addActor(gameRestartButton);
  
      }
  
      @Override
      public void render(float delta) {
  
          Gdx.gl.glClearColor(0, 0, 0, 0);
          Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
  
          camera.update();
  
          gameOverBatch.begin();
          bitmapLogoFont.setColor(Color.RED);
          bitmapLogoFont.draw(gameOverBatch, "Game Over", w / 2 - 210, h / 2 + 150f);
          gameOverBatch.end();
  
          exitGameButton.act(Gdx.graphics.getDeltaTime());
  
          stage.act(Gdx.graphics.getDeltaTime());
          stage.draw();
  
      }
  
      @Override
      public void resize(int width, int height) {
  
      }
  
      @Override
      public void show() {
  
      }
  
      @Override
      public void hide() {
  
      }
  
      @Override
      public void pause() {
  
      }
  
      @Override
      public void resume() {
  
      }
  
      @Override
      public void dispose() {
          gameOverFontGen.dispose();
  
      }
  }
  </code></pre>
  
  <p>In GameOverScreen constructor I have two buttons (<code>exitGameButton</code> and <code>gameReturnButton</code>) I'd like to be able to trigger their event-listeners, but I can't seem to be able to do it. Neither of my buttons responds to the event listener. What am I doing wrong? Do I need to add something else on to my <code>MainActivity</code> class for these buttons to work ? Thanks much.</p>