How do I manage the back buttons for multiple screens in libgdx?

What if there are some ways to manage the back button in libgdx?

For example, in andengine, I implemented the following:

@Override
public boolean onKeyDown(int keyCode,KeyEvent event) {  
     if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {          
          switch (currentScene) {
               case SPLASH:
                   break;
               case MENU:
                   Process.killProcess(Process.myPid());
                   break;
               case WORLDMENU:
                       start(MENU);
                       break;
...
...
    }
  }
}

I don't know how to do it here, because applicationlistener only has create, show, render... I've tried this:

if (Gdx.input.isButtonPressed(Keys.BACK)){
    new ScreenChangeTask(MyScreen.SPLASH);
}

But it still closes my application

For reference only: I have a class controller extension game. I use public void setscreen to switch between screens

Solution

To do this correctly, you need to tell libgdx to capture the back key:

Gdx.input.setCatchBackKey(true);

You should do this somewhere early in the application If you want users to be able to use the back key, set it to false

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>