Game Development Reference
In-Depth Information
2.
We won't be able to display the destruction event of our activity in the
TextView
because it will vanish from the screen too fast, so we will also
output all state changes to LogCat. We do this with the
Log
class, which
provides a couple of static methods with which to append messages
to LogCat.
Remember what we need to do to add a test activity to our test application. First, we define it
in the manifest file in the form of an
<activity>
element, which is a child of the
<application>
element:
<activity android:label="
Life Cycle Test
"
android:name=".
LifeCycleTest
"
android:configChanges="
keyboard|keyboardHidden|orientation
" />
Next we add a new Java class called
LifeCycleTest
to our
com.badlogic.androidgames
package. Finally, we add the class name to the
tests
member of the
AndroidBasicsStarter
class we defined earlier. (Of course, we already have that in there from when we wrote the class
for demonstration purposes.)
We'll have to repeat all of these steps for any test activity that we create in the following
sections. For brevity, we won't mention these steps again. Also note that we didn't specify an
orientation for the
LifeCycleTest
activity. In this example, we can be in either landscape mode
or portrait mode, depending on the device orientation. We did this so that you can see the effect
of an orientation change on the life cycle (none, due to how we set the
configChanges
attribute).
Listing 4-2 shows the code of the entire activity.
Listing 4-2. LifeCycleTest.java, Demonstrating the Activity Life Cycle
package
com.badlogic.androidgames;
import
android.app.Activity;
import
android.os.Bundle;
import
android.util.Log;
import
android.widget.TextView;
public class
LifeCycleTest
extends
Activity {
StringBuilder builder =
new
StringBuilder();
TextView textView;
private void
log(String text) {
Log.
d
("LifeCycleTest", text);
builder.append(text);
builder.append('\n');
textView.setText(builder.toString());
}
@Override
public void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
textView =
new
TextView(
this
);
textView.setText(builder.toString());