An Android application can contain zero or more activities. When your application has more than one activity, you often need to navigate from one to another. In Android, you navigate between activities through what isknown as an intent.
The best way to understand this very important but somewhat abstract concept in Android is to experience it firsthand and see what it helps you to achieve. The following Try It Out shows how to add another activity to anexisting project and then navigate between the two activities.
1. Using Eclipse, create a new Android project and name it UsingIntent.
2. Right-click on the package name under the src folder and select New ➪ Class (see Figure 2-9).
Resolving Intent Filter Collision
FIGURE 2-9
3. Name the new class SecondActivity and click Finish.
4. Add the following statements in bold to the AndroidManifest.xml file:
<?xml version=”1.0” encoding=”utf-8”?>
package=”net.learn2develop.UsingIntent” android:versionCode=”1” android:versionName=”1.0” >
<uses-sdk android:minSdkVersion=”14” />
<application android:icon=”@drawable/ic_launcher” android:label=”@string/app_name” >
<activity android:label=”@string/app_name” android:name=”.UsingIntentActivity” >
<intent-filter >
<action android:name=”android.intent.action.MAIN” />
<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
<activity
android:label=”Second Activity”
android:name=”.SecondActivity” >
<intent-filter >
<action android:name=”net.learn2develop.SecondActivity” />
<category android:name=”android.intent.category.DEFAULT” />
</intent-filter>
</activity>
</application>
</manifest>
5. Make a copy of the main.xml file (in the res/layout
folder) by right-clicking on it and selecting Copy.
Then, right-click on the res/layout folder and select Paste.
Name the file secondactivity.xml. The res/layout folder will now contain the secondactivity.xml file (see Figure 2-10).
FIGURE 2-10
6. Modify the secondactivity.xml file as follows:
<?xml version=”1.0” encoding=”utf-8”?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_width=”fill_parent” android:layout_height=”fill_parent”
android:orientation=”vertical” >
<TextView android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”This is the Second Activity!” />
</LinearLayout>
7. In the SecondActivity.java file, add the following statements in bold:
package net.learn2develop.UsingIntent;
import android.app.Activity;
import android.os.Bundle;
public class SecondActivity extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.secondactivity);
}
}
8. Add the following lines in bold to the main.xml file:
<?xml version=”1.0” encoding=”utf-8”?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_width=”fill_parent” android:layout_height=”fill_parent”
android:orientation=”vertical” >
<Button
android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”Display second activity” android:onClick=”onClick”/>
</LinearLayout>
9. Modify the UsingIntentActivity.java file as shown in bold:
package net.learn2develop.UsingIntent;
import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View;
public class UsingIntentActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);
}
public void onClick(View view) {
startActivity(new Intent(“net.learn2develop.SecondActivity”));
}
}
10. Press F11 to debug the application on the Android emulator. When the first activity is loaded, click the button and the second activity will now be loaded (see Figure 2-11).
FIGURE 2-11
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, UsingIntentActivity.java). Hence, if you want to add another activity to a project, you need to create these twocomponents.
In the AndroidManifest.xml file, specifically you have added the following:
<activity
android:label=” Second Activity”
android:name=”.SecondActivity” >
<intent-filter >
<action android:name=”net.learn2develop.SecondActivity” />
<category android:name=”android.intent.category.DEFAULT” />
</intent-filter>
</activity>
Here, you have added a new activity to the application. Note the following:
➤ The name (class) of the new activity added is SecondActivity.
➤ The label for the new activity is named Second Activity.
➤ The intent filter name for the new activity is net.learn2develop.SecondActivity. 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 intentfilter name in order to reduce the chances of another application having the same intent filter name. (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).
When the button is clicked, you use the startActivity() method to display SecondActivity by creating an instance of the Intent class and passing it the intent filter name of SecondActivity (which is net.learn2develop.SecondActivity):
public void onClick(View view) {
startActivity(new Intent(“net.learn2develop.SecondActivity”));
}
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 SecondActivity by using its net.learn2develop
.S econdActivity 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(new Intent(this, SecondActivity.class));
However, this approach is applicable only when the activity you want to display is within the same project as the current activity.
Resolving Intent Filter Collision
In the previous section, you learned that the <intent-filter> element defines how your activity can be invoked by another activity. What happens if another activity (in either the same or a separate application) has the same filtername? For example, suppose your application has another activity named Activity3, with the following entry in the AndroidManifest.xml file:
<?xml version=“1.0“ encoding=“utf-8“?>
package=”net.learn2develop.UsingIntent” android:versionCode=”1” android:versionName=”1.0” >
<uses-sdk android:minSdkVersion=”14” />
<application android:icon=”@drawable/ic_launcher” android:label=”@string/app_name” >
<activity android:label=”@string/app_name” android:name=”.UsingIntentActivity” >
<intent-filter >
<action android:name=”android.intent.action.MAIN” />
<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
<activity
android:label=”Second Activity”
android:name=”.SecondActivity” >
<intent-filter >
<action android:name=”net.learn2develop.SecondActivity” />
<category android:name=”android.intent.category.DEFAULT” />
</intent-filter>
</activity>
<activity
android:label=”Third Activity”
android:name=”.ThirdActivity” >
<intent-filter >
<action android:name=”net.learn2develop.SecondActivity” />
<category android:name=”android.intent.category.DEFAULT” />
</intent-filter>
</activity>
</application>
</manifest>
If you call the startActivity() method with the following intent, then the Android OS will displaya selection of activities, as shown in Figure 2-12:
startActivity(new Intent(“net.learn2develop.SecondActivity”));
FIGURE 2-12
If you check the “Use by default for this action” item and then select an activity, then the nexttime the intent “net.learn2develop.SecondActivity” is called again, it will launch the previous activity that you have selected.
To clear this default, go to the Settings application in Android and select Apps ➪ Manage applications, and then select the application name (see Figure 2-13). When the details of the application are shown, scroll down tothe bottom and click the Clear defaults button.
FIGURE 2-13
Không có nhận xét nào:
Đăng nhận xét