I want to start a game from a menu. In Eclipse, I have 2 projects, one with the menu, the other the actual game. Both using SimpleBaseGameActivity
as their base. The examples on the net do something like below. In particular, it creates an intent and starts an activity with that intent. The code below gives a NoClassDefFoundError
on MyGame.class
. This is no surprise since MyGame.class
doesn't exist, but rather MyGame.apk
does. How do I do this?
public boolean onMenuItemClicked(final MenuScene pMenuScene,
final IMenuItem pMenuItem,
final float pMenuItemLocalX,
final float pMenuItemLocalY) {
switch(pMenuItem.getID()) {
case MENU_PLAY:
MainActivity.this.runOnUiThread(new Runnable() {
public void run() {
Intent intent = new Intent(getApplication(), MyGame.class);
startActivity(intent);
finish();
}
});
return true;
}
}
----- edit
I've got it working, with everything in one project, in that when the menu item
is clicked on, then the game starts. However, when the 'back arrow' is clicked, it doesn't return to the menu
, but rather to the operating system. The activity definitions in the manifest
file are below. Does this look correct?
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.mygame.MyGame"
android:label="@string/mygame_activity"
android:parentActivityName="com.menu.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.menu.MainActivity" />
</activity>
I added this to MyGame
, but it doesn't get called:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
}
--- edit I needed to remove this line:
MainActivity.this.finish();
MyGame have to be an activity and it must be mentioned in Android.manifest as an activity.
http://developer.android.com/training/basics/firstapp/starting-activity.html