Game Development Reference
In-Depth Information
which takes two arguments: a
Context
instance and a
Class
instance. The latter represents the
Java class of the activity we want to start.
Context
is an interface that provides us with global information about our application. It is
implemented by the
Activity
class, so we simply pass this reference to the
Intent
constructor.
To get the
Class
instance representing the activity we want to start, we use a little reflection,
which will probably be familiar to you if you've worked with Java. Reflection allows us to
programmatically inspect, instantiate, and call classes at runtime. The static method
Class.forName()
takes a string containing the fully qualified name of a class for which we want
to create a
Class
instance. All of the test activities we'll implement later will be contained in the
com.badlogic.androidgames
package. Concatenating the package name with the class name we
fetched from the
tests
array will give us the fully qualified name of the activity class we want to
start. We pass that name to
Class.forName()
and get a nice
Class
instance that we can pass to
the
Intent
constructor.
Once the
Intent
instance is constructed, we can start it with a call to the
startActivity()
method. This method is also defined in the
Context
interface. Because our activity implements
that interface, we just call its implementation of that method. And that's it!
So how will our application behave? First, the starter activity will be displayed. Each time we
touch an item on the list, the corresponding activity will be started. The starter activity will be
paused and will go into the background. The new activity will be created by the intent we
send out and will replace the starter activity on the screen. When we press the back button on
the Android device, the activity is destroyed and the starter activity is resumed, taking back
the screen.
Creating the Test Activities
When we create a new test activity, we have to perform the following steps:
Create the corresponding Java class in the
com.badlogic.androidgames
package and implement its logic.
1.
2.
Add an entry for the activity in the manifest file, using whatever attributes
it needs (that is,
android:configChanges
or
android:screenOrientation
).
Note that we won't specify an
<intent-filter>
element, as we'll start the
activity programmatically.
3. Add the activity's class name to the
tests
array of the
AndroidBasicsStarter
class.
As long as we stick to this procedure, everything else will be taken care of by the logic we
implemented in the
AndroidBasicsStarter
class. The new activity will automatically show up in
the list, and it can be started by a simple touch.
One thing you might wonder is whether the test activity that gets started on a touch is running in
its own process and VM. It is not. An application composed of activities has something called an
activity stack
. Every time we start a new activity, it gets pushed onto that stack. When we close
the new activity, the last activity that got pushed onto the stack will get popped and resumed,
becoming the new active activity on the screen.