Recipe Launching Another Activity from an Event

In this recipe, MenuScreen is the main activity as shown in Listing 2.8. It launches the PlayGame activity. Here the trigger event is implemented as a button click using the Button widget.

When a user clicks the button, the startGame() function runs; it launches the PlayGame activity. When a user clicks the button in the PlayGame activity, it calls finish() to return control to the calling activity. The steps for launching an activity are

1. Declare an Intent that points to the activity to be launched.

2. Call startActivity on this intent.

3. Declare the additional activity in the AndroidManifest. Listing 2.8 src/com/cookbook/launch_activity/MenuScreen.java package com.cookbook.launch_activity;

import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button;

public class MenuScreen extends Activity {

@Override public void onCreate(Bundle savedlnstanceState) { super.onCreate(savedlnstanceState); setContentView(R.layout.main);

//setup button listener

Button startButton = (Button) findViewById(R.id.play_game); startButton.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { startGame();

private void startGame() {

Intent launchGame = new Intent(this, PlayGame.class); startActivity(launchGame);

Provide Current Context in an Anonymous Inner Class

Note the additional consideration needed for launching an activity with a Button press, as shown in Listing 2.8. The intent needs a context. However, using the this shortcut in the onClick function is not properly resolved. Different ways to provide current context in an anonymous inner class are

■ Use Context.this instead of this.

■ Use getApplicationContext() instead of this.

■ Explicitly use the class name MenuScreen.this.

Call a function that is declared at the right context level. This is what is used in Listing 2.8: startGame().

Each of these methods can usually be interchanged. Utilize the one that works best for the clarity of the situation.

The PlayGame activity shown in Listing 2.9 is simply a button with a onClick listener that calls finish() to return control to the main activity. More functionality can be added as needed to this activity, and multiple branches of the code can each lead to their own finish() calls.

Listing 2.9 src/com/cookbook/launch_activity/PlayGamejava package com.cookbook.launch_activity;

import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button;

public class PlayGame extends Activity {

public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.game);

//setup button listener

Button startButton = (Button) findViewById(R.id.end_game); startButton.setOnClickListener(new View.OnClickListener() {

public void onClick(View view) { finish();

The button must be added to the main layout as shown in Listing 2.10, with the ID

play_game to match what was declared in Listing 2.8. Here, the size of the button is also declared in device-independent pixels (dip), as discussed more in Chapter 4.

Listing 2.10 res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent"

android:layout_height="fill_parent" >

<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" />

<Button android:id="@+id/play_game"

android:layout_width="100dip" android:layout_height="100dip" android:text="@string/play_game" />

</LinearLayout>

The PlayGame activity references its own button ID end_game in the R.layout.game layout resource that corresponds to the layout XML file game.xml, as shown in Listing 2.11.

Listing 2.11 res/layout/game.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent"

android:layout_height="fill_parent" >

<Button android:id="@+id/end_game"

android:layout_width="100dip" android:layout_height="100dip" android:text="@string/end_game" android:layout_centerInParent="true" />

</LinearLayout>

Although the text can be written explicitly in each case, it is good coding practice to define variables for each string. In this recipe, the two string values play_game and end_game need to be declared in the string XML resource file, as shown in Listing 2.12.

Listing 2.12 res/values/strings.xml

<?xml version="1.0" encoding="utf-8"?> <resources>

<string name="hello">This is the Main Menu</string> <string name="app_name">LaunchActivity</string> <string name="play_game">Play game?</string> <string name="end_game">Done?</string> </resources>

Finally, the AndroidManifest XML file needs to register a default action to the new class PlayGame, as shown in Listing 2.13.

Listing 2.13 AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1"

android:versionName="1.0" package="com.cookbook.launch_activity"> <application android:icon="@drawable/icon"

android:label="@string/app_name"> <activity android:name=".MenuScreen"

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=".PlayGame"

android:label="@string/app_name"> <intent-filter>

<action android:name="android.intent.action. VIEW" /> <category android:name="android.intent.category. DEFAULT" /> </intent-filter> </activity>

</application>

<uses-sdk android:minSdkVersion="3" /> </manifest>

Character Building Thought Power

Character Building Thought Power

Character-Building Thought Power by Ralph Waldo Trine. Ralph draws a distinct line between bad and good habits. In this book, every effort is made by the writer to explain what comprises good habits and why every one needs it early in life. It draws the conclusion that habits nurtured in early life concretize into impulses in future for the good or bad of the subject.

Get My Free Ebook


Post a comment