Reputation: 2556
I'm using libGdx with multiple screens. I want to use the back key on my android device to back to the previous screen similar to how android goes back to the prevvious activity by default.
My current way of doing this is to have every screen get the screen it was called from. Then I'd implement the InputProcessor in every Screen and use it to receive back key presses and then change the screen to the one that was shown before.
While this works, it sucks when working with multiple screens because essentially one has to rewrite the code over and over again.
Does anyone know a more elegant way of achieving this? Does libGdx maybe even have this function by default and I just don't find it? Or do I need to do it this way.
Thanky you for answering,
Cheers,
Tony
Upvotes: 1
Views: 67
Reputation: 10320
My current way of doing this is to have every screen get the screen it was called from. Then I'd implement the InputProcessor in every Screen and use it to receive back key presses and then change the screen to the one that was shown before.
This is how I do it. I dont see any other more elegant solution for this. And the code you must rewrite in every screen is minimal:
switch(keycode){
...
case Keys.BACK:
game.setScreen(previousScreen);
return true;
}
(game is your ApplicationListener, previousScreen is the screen that called the current one, and keycode is the int given in the keydown/up methods of the InputListener)
Upvotes: 1