try it out
1 .
Using the Activities project created earlier, add the following statements in bold to the AndroidManifest.xml fi le:<?xmlversion=”1.0”encoding=”utf-8”?>
<manifestxmlns:android=”http://schemas.android.com/apk/res/android”
package=”net.learn2develop.Activities”
android:versionCode=”1”
android:versionName=”1.0”>
<applicationandroid:icon=”@drawable/icon”android:label=”@string/app_name”>
<activityandroid:name=”.MainActivity”
android:label=”@string/app_name”
android:theme=”@android:style/Theme.Dialog” > <intent-filter>
<actionandroid:name=”android.intent.action.MAIN”/>
<categoryandroid:name=”android.intent.category.LAUNCHER”/>
</intent-filter>
</activity>
<activity android:name=”.Activity2”
android:label=”Activity 2”> <intent-filter>
<action android:name=”net.learn2develop.ACTIVITY2” /> <category android:name=”android.intent.category.DEFAULT” /> </intent-filter>
</activity> </application>
<uses-sdkandroid:minSdkVersion=”9”/>
NOTE You’ll need to remove the attribute that has strikethrough applied.
2 .
Right click on the package name under the src folder and select New ➪ Class (see Figure 2-10).4 .
Make a copy of the main.xml file by right-clicking on it and selecting Copy. Then, right-click on the res/layout folder and select Paste. Name the file activity2.xml. The res/layout folder will now contain the activity2.xml file (see Figure 2-12).Figure 2-10
5 .
Modify the activity2.xml file as follows:<?xmlversion=”1.0”encoding=”utf-8”?>
<LinearLayoutxmlns: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=”This is Activity 2!”
/>
</LinearLayout>
6 .
In the Activity2.java file, add the following statements in bold:packagenet.learn2develop.Activities; import android.app.Activity;
import android.os.Bundle;
public class Activity2 extends Activity { @Override
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.activity2); }
}
7 .
Modify the MainActivity.java file as shown in bold:packagenet.learn2develop.Activities; importandroid.app.Activity; importandroid.os.Bundle; importandroid.util.Log; importandroid.view.Window; importandroid.view.KeyEvent; importandroid.content.Intent;
publicclassMainActivityextendsActivity{ Stringtag=“Events”;
/**Calledwhentheactivityisfirstcreated.*/ @Override
publicvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); //---hidesthetitlebar--- //requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.main); Log.d(tag,“IntheonCreate()event”); }
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
}
return false; }
publicvoidonStart(){//...} publicvoidonRestart(){//...} publicvoidonResume(){//...} publicvoidonPause(){//...} publicvoidonStop(){//...} publicvoidonDestroy(){//...} }
8 .
Press F11 to debug the application on the Android Emulator. When the first activity is loaded, click the center of the directional pad (see Figure 2-13; on a real device this can be achieved by pressing down the trackball). The second activity will now be loaded.Figure 2-13
How It Works
As you have learned, an activity is made up of a UI component (for example, main.xml) and a class component (for example, MainActivity.java). Hence, if you want to add another activity to a project, you need to create these two components.
In the AndroidManifest.xml file, specifically you have added the following: <activityandroid:name=”.Activity2”
<intent-filter>
<actionandroid:name=”net.learn2develop.ACTIVITY2”/>
<categoryandroid:name=”android.intent.category.DEFAULT”/>
</intent-filter>
</activity>
Here, you have added a new activity to the application. Note the following: The name of the new activity added is “
➤
➤ Activity2”.
The label for the activity is named “ ➤
➤ Activity2”.
The intent filter name for the activity is “ ➤
➤ net.learn2develop.ACTIVITY2”. Other activities that wish
to call this activity will invoke it via this name. Ideally, you should use the reverse domain name of your company as the intent filter name in order to reduce the chances of another application having the same intent filter. The next section discusses what happens when two or more activities have the same intent filter.
The category for the intent filter is “ ➤
➤ android.intent.category.DEFAULT”. You need to add this to the intent filter so that this activity can be started by another activity using the startActivity() method (more on this shortly).
In the MainActivity.java file, you implemented the onKeyDown event handler. This event is fired when- ever the user presses one of the keys on the device. When the user presses the center key on the direc- tional pad (as represented by the KeyEvent.KEYCODE_DPAD_CENTER constant), you use the startActivity() method to display Activity2 by creating an instance of the Intent class and passing it the intent filter name of Activity2 (which is net.learn2develop.ACTIVITY2):
publicbooleanonKeyDown(intkeyCode,KeyEventevent) {
if(keyCode==KeyEvent.KEYCODE_DPAD_CENTER) {
startActivity(new Intent(“net.learn2develop.ACTIVITY2”)); }
returnfalse; }
Activities in Android can be invoked by any application running on the device. For example, you can create a new Android project and then display Activity2 by using its net.learn2develop.ACTIVITY2 intent filter. This is one of the fundamental concepts in Android that enables an application to invoke another easily.
If the activity that you want to invoke is defined within the same project, you can rewrite the preceding statement like this:
startActivity(newIntent(this,Activity2.class));
However, this approach is applicable only when the activity you want to display is within the same project as the current activity.