I am making a Android game with LibGDX (using ADT eclipse bundle). How do I choose the default activity to launch when the user opens the app. I set the project up with the LibGDX setup GUI, and I can't find the main activity. I want a main menu to open before the game starts, is there something I should change in the manifest? Thanks in advance.
2 Answers
If you are using LibGDX, you need to forego the concept of Activities and Views, since your entire game will now just be a single Activity. To have a main menu when you game starts up doesn't switch the Activity, but just presents a different set of objects to be rendered to the same Activity.
The easiest way to accomplish this would be to do the following. You should have a .java file in your -core project named after your game name. It would be a class extending ApplicationListener
. Now, change it to extend Game
instead, which should be in the package com.badlogic.gdx
. If you look at the logic of the Game
class, you will see it has a setScreen
function. Once a Screen
has been set, it simply passes all the method calls like render()
and resize()
to a given screen.
Thus, to have different screens, just create a new class that extends com.badlogic.gdx.Screen
. You can do this for any number of screens you need (e.g., MainMenuScreen, GameScreen). Whenever you need to switch screens (e.g., going from Main Menu -> Game), you can just call the setScreen
function from your game.
The reason why you should take this approach, and not mess with the manifest.xml and adding additional activities, is because in this way your game is cross-platform. Your main menu will not just be specific to the Android build, and can be used across desktop, ios, and html too.
-
1\$\begingroup\$ +1, nevertheless : For a simple game, it's often not the case, but if you want to integrate admob, or use top bar notifications or others platform-specific stuff that are not abstracted by libgdx, you may have to deal with Views, Layout and Services for example, so you can't totally forget about the Android SDK. \$\endgroup\$– KhopaMay 20, 2014 at 11:09
-
\$\begingroup\$ @midasmax Thanks for the advice. Currently, I have the main game class and the main menu class that implements screen. In the main menu class, how do I make it display the menu (I assume with the show() method)... What do I pass into the show() method to make it display the main menu? At least for the Android game build, I'd want it to be stock UI made w/ an XML layout file. Thanks. \$\endgroup\$ May 20, 2014 at 21:47
-
\$\begingroup\$ @Khopa thanks for the advice :) I will be sure to keep that in mind. \$\endgroup\$ May 20, 2014 at 21:47
-
1\$\begingroup\$ Sorry that I didn't get back to this. If you still want to make use of a layout created using Stock UI and XML files, then I'm afraid you're going to have to modify things quite a bit. As @Khopa mentioned, this should really be reserved for doing things that are API specific like implementing ads. The libGDX article on integrating AdMob is what you'll need to look at, as it creates a custom view to handle the AdMob Activity. \$\endgroup\$– midasmaxMay 23, 2014 at 21:30
-
\$\begingroup\$ @just_a_programmer I know I mentioned this before, but you should really consider using libGDX's scene2d tools to create your GUIs. They are pretty powerful, and not too far away from how Android's XML layouts work. What's more, they are cross-platform, so you can make use of them across your projects. For example, here is how you create various GUIs using Scene2D Layouts \$\endgroup\$– midasmaxMay 23, 2014 at 21:46
try to invalidate cache first.Then try other options
-
3\$\begingroup\$ This answer would be better if it explained why invalidating the cache could help, how to invalidate the cache, and what other options should be tried. \$\endgroup\$– DMGregory ♦Sep 26, 2020 at 16:57