2011年5月20日金曜日

AndroidStudyMemo=an application launches multiple Activities

Adding this code to AndroidManifest.xml enables Android to pass Intents for the Test

Activity to the correct place. The full AndroidManifest.xml file should look like this:

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

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

                            package="android_programmers_guide.AndroidViews">

                            <application android:icon="@drawable/icon">

 

                                          <activity android:name=".AndroidViews" 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=".AutoComplete" android:label="AutoComplete">

                                                        <intent-filter>

                                                                      <action android:name="android.intent.action.MAIN" />

                                                                      <category android:name="android.intent.category.LAUNCHER"/>

                                                        </intent-filter>

                                          </activity>

 

                            </application>

              </manifest>

 

Now your Activity can handle Intent calls for the Test Activity. To make your Intent

call to the Test Activity, you are going to use a structure very similar to the one you used

when calling the phone dialer in Chapter 7. The following line of code will set up your

Intent:

              Intent testActivity = new Intent(this, test.class);

 

This line creates an Intent called testActivity. The parameter test.class tells the call

that you want the Intent testActivity to represent the Test Activity you created that is

associated with this Activity.

 

Finally, use the startActivity( ) method to actually start the Test Activity:

              startActivity(autocomplete);

 

Your completed AndroidViews.java file should look like this:

              package android_programmers_guide.AndroidViews;

              import android.app.Activity;

              import android.os.Bundle;

              import android.view.Menu;

              import android.content.Intent;

              public class AndroidViews extends Activity {

                            /** Called when the Activity is first created. /

                            @Override

                            public void onCreate(Bundle icicle) {

                                          super.onCreate(icicle);

                                          setContentView(R.layout.main);

                                          /**Set up our Intent /

 

Run this application in your Android Emulator. Android should launch the

AndroidViews Activity, followed quickly by the Test Activity.

In the following section, you will use these techniques to create an application that

launches multiple Activities. Each of these Activities will house one View to which you

can apply different options. This will give you a great deal of practice displaying and

manipulating Views as well as working with Activities.

 

0 件のコメント:

コメントを投稿