Game Development Reference
In-Depth Information
However, remember that we only have one main activity. So, what does our main activity look
like? We want a convenient way to add new activities, and we want the ability to start a specific
activity easily. With one main activity, it should be clear that that activity will somehow provide
us with a means to start a specific test activity. As discussed earlier, the main activity will be
specified as the main entry point in the manifest file. Each additional activity that we add will be
specified without the
<intent-filter>
child element. We'll start those programmatically from the
main activity.
The AndroidBasicsStarter Activity
The Android API provides us with a special class called
ListActivity
, which derives from the
Activity
class that we used in the Hello World project. The
ListActivity
class is a special
type of activity whose single purpose is to display a list of things (for example, strings).
We use it to display the names of our test activities. When we touch one of the list items,
we'll start the corresponding activity programmatically. Listing 4-1 shows the code for our
AndroidBasicsStarter
main activity.
Listing 4-1. AndroidBasicsStarter.java, Our Main Activity Responsible for Listing and Starting All Our Tests
package
com.badlogic.androidgames;
import
android.app.ListActivity;
import
android.content.Intent;
import
android.os.Bundle;
import
android.view.View;
import
android.widget.ArrayAdapter;
import
android.widget.ListView;
public class
AndroidBasicsStarter
extends
ListActivity {
String tests[] = { "LifeCycleTest", "SingleTouchTest", "MultiTouchTest",
"KeyTest", "AccelerometerTest", "AssetsTest",
"ExternalStorageTest", "SoundPoolTest", "MediaPlayerTest",
"FullScreenTest", "RenderViewTest", "ShapeTest", "BitmapTest",
"FontTest", "SurfaceViewTest" };
public void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setListAdapter(
new
ArrayAdapter<String>(
this
,
android.R.layout.
simple_list_item_1
, tests));
}
@Override
protected void
onListItemClick(ListView list, View view,
int
position,
long
id) {
super
.onListItemClick(list, view, position, id);
String testName = tests[position];
try
{
Class clazz = Class
.forName
("com.badlogic.androidgames." + testName);