Android Manifest File for a Game
Posted by Dimitri | Dec 15th, 2010 | Filed under Programming
Android was designed to see every application as a collection of Activities united by intents. It also relies on the Activity stack to determine what Activity will be launched after the user finishes it by pressing the ‘back’ button. While the stack system is helpful for users and for some applications, this default behavior isn’t a necessarily a good thing for games.
That is mainly because of two things: a game use a lot of hardware resources from the device that runs it, meaning that having other activities in the same stack as the game Activity can have an impact on its performance. And the stack default behavior could lead to multiple instances of the same game running in the same stack.
So how to prevent these problems from happening? Basically, the game Activity must not share its stack with other unrelated ones and must be the only one of its type for a given stack. Fortunately, this can be easily achieved by editing the Android Manifest file.
To exemplify, let’s assume we wanted to program a game with only three Activities represented by this diagram:
The arrows indicate how these Activities are called from one to the other. In this example, the first Activity launched is the Main Menu. This is the Manifest file for this game:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="fortyonepost.game" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name" android:launchMode="singleInstance"> <activity android:name=".MainMenu" android:configChanges = "orientation|keyboardHidden" android:multiprocess="false" android:screenOrientation="landscape" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".Game" android:clearTaskOnLaunch="true" <!--All activities are removed from the stack, except the root Activity--> android:multiprocess="false" <!--This activity can't be instantiated in multiple processes--> android:configChanges = "orientation|keyboardHidden" <!--Tells Android that the Activity will handle these events--> android:screenOrientation="landscape" <!--Forces the game to run in landscape mode--> android:theme="@android:style/Theme.NoTitleBar.Fullscreen" <!--Removes the title bar--> android:label="@string/app_name"> </activity> <activity android:name=".PauseMenu" android:configChanges = "orientation|keyboardHidden" android:multiprocess="false" android:screenOrientation="landscape" android:label="@string/app_name"> </activity> </application> <uses-sdk android:minSdkVersion="3"/> </manifest>
It works like this: on line 8, the application is declared as a single instance, so it becomes the only Activity in the stack. Every time the game Activity is launched, it clears the other Activities that were on the stack, such as the Pause Menu or the Main Menu. That’s why clearTaskOnLaunch mode was set on line 23. This avoids multiple instances of the same game. Line 24 makes the Activity run in a single process.
Basically, this XML makes the game use its own exclusive stack not letting other unrelated activities use it. Also, the stack is cleared every time the game Activity is started. Plus, it makes the game run in a single process, avoiding the two problems that where presented in the beginning of this post.
And that’s it.
Be the first to leave a comment!