Vous êtes sur la page 1sur 11

10/14/13

Starting Another Activity | Android Developers

Starting Another Activity


After completing the previous lesson [1], you have an app that shows an activity (a single screen) with a text field and a button. In this lesson, youll add some code to M a i n A c t i v i t y that starts a new activity when the user clicks the Send button.

Respond to the Send Button


To respond to the button's on-click event, open the a c t i v i t y _ m a i n . x m l layout file
2 ]element: and add the a n d r o i d : o n C l i c k attribute to the < B u t t o n >[

< B u t t o n a n d r o i d : l a y o u t _ w i d t h = " w r a p _ c o n t e n t " a n d r o i d : l a y o u t _ h e i g h t = " w r a p _ c o n t e n t " a n d r o i d : t e x t = " @ s t r i n g / b u t t o n _ s e n d " a n d r o i d : o n C l i c k = " s e n d M e s s a g e "/ >

The a n d r o i d : o n C l i c k attributes value, " s e n d M e s s a g e " , is the name of a method in your activity that the system calls when the user clicks the button. Open the M a i n A c t i v i t y class (located in the project's s r c / directory) and add the corresponding method:
/ * *C a l l e dw h e nt h eu s e rc l i c k st h eS e n db u t t o n* / p u b l i cv o i ds e n d M e s s a g e ( V i e wv i e w ){ / /D os o m e t h i n gi nr e s p o n s et ob u t t o n }
3 ]class: This requires that you import the V i e w[

i m p o r ta n d r o i d . v i e w . V i e w ;

Tip: In Eclipse, press Ctrl + Shift + O to import missing classes (Cmd + Shift + O on Mac).
developer.android.com/training/basics/firstapp/starting-activity.html 1/11

10/14/13

Starting Another Activity | Android Developers

In order for the system to match this method to the method name given to
a n d r o i d : o n C l i c k , the signature must be exactly as shown. Specifically, the

method must: Be public Have a void return value


4 ]as the only parameter (this will be the V 5 ]that was Have a V i e w[ i e w[

clicked) Next, youll fill in this method to read the contents of the text field and deliver that text to another activity.

Build an Intent
6 ]is an object that provides runtime binding between separate An I n t e n t[ 7 ]represents an apps "intent components (such as two activities). The I n t e n t[

to do something." You can use intents for a wide variety of tasks, but most often theyre used to start another activity.
8 ]to start an activity called Inside the s e n d M e s s a g e ( ) method, create an I n t e n t[

D i s p l a y M e s s a g e A c t i v i t y : I n t e n ti n t e n t=n e wI n t e n t ( t h i s ,D i s p l a y M e s s a g e A c t i v i t y . c l a s s ) ;

The constructor used here takes two parameters:


9 ]as its first parameter (t 1 0 ]class AC o n t e x t[ h i s is used because the A c t i v i t y[ 1 1 ] is a subclass of C o n t e x t[ ) 1 2 ]of the app component to which the system should deliver the The C l a s s[ 1 3 ](in this case, the activity that should be started) I n t e n t[

Sending an intent to other apps


The intent created in this lesson is what's considered an explicit intent, because
1 4 ]specifies the exact app component to which the intent should be the I n t e n t[ 1 5 ]does given. However, intents can also be implicit, in which case the I n t e n t[

not specify the desired component, but allows any app installed on the device
developer.android.com/training/basics/firstapp/starting-activity.html 2/11

10/14/13

Starting Another Activity | Android Developers

to respond to the intent as long as it satisfies the meta-data specifications for


1 6 ]parameters. For more the action that's specified in various I n t e n t[

information, see the class about Interacting with Other Apps [17]. Note: The reference to D i s p l a y M e s s a g e A c t i v i t y will raise an error if youre using an IDE such as Eclipse because the class doesnt exist yet. Ignore the error for now; youll create the class soon. An intent not only allows you to start another activity, but it can carry a bundle of data to the activity as well. Inside the s e n d M e s s a g e ( ) method, use f i n d V i e w B y I d ( )
1 8 ]element and add its text value to the intent: to get the E d i t T e x t[

I n t e n ti n t e n t=n e wI n t e n t ( t h i s ,D i s p l a y M e s s a g e A c t i v i t y . c l a s s ) ; E d i t T e x te d i t T e x t=( E d i t T e x t )f i n d V i e w B y I d ( R . i d . e d i t _ m e s s a g e ) ; S t r i n gm e s s a g e=e d i t T e x t . g e t T e x t ( ) . t o S t r i n g ( ) ; i n t e n t . p u t E x t r a ( E X T R A _ M E S S A G E ,m e s s a g e ) ;

Note: You now need import statements for a n d r o i d . c o n t e n t . I n t e n t and


a n d r o i d . w i d g e t . E d i t T e x t . You'll define the E X T R A _ M E S S A G E constant in a moment.
1 9 ]can carry a collection of various data types as key-value pairs An I n t e n t[

called extras. The p u t E x t r a ( ) method takes the key name in the first parameter and the value in the second parameter. In order for the next activity to query the extra data, you should define the key for your intent's extra using a public constant. So add the E X T R A _ M E S S A G E definition to the top of the M a i n A c t i v i t y class:
p u b l i cc l a s sM a i n A c t i v i t ye x t e n d sA c t i v i t y{ p u b l i cf i n a ls t a t i cS t r i n gE X T R A _ M E S S A G E=" c o m . e x a m p l e . m y f i r s t a p p . M E S S A G E " ; . . . }

It's generally a good practice to define keys for intent extras using your app's package name as a prefix. This ensures they are unique, in case your app interacts with other apps.

Start the Second Activity


developer.android.com/training/basics/firstapp/starting-activity.html 3/11

10/14/13

Starting Another Activity | Android Developers

2 0 ] To start an activity, call s t a r t A c t i v i t y ( ) and pass it your I n t e n t[ . The system 2 1 ]specified by the receives this call and starts an instance of the A c t i v i t y[ 2 2 ] I n t e n t[ .

With this new code, the complete s e n d M e s s a g e ( ) method that's invoked by the Send button now looks like this:
/ * *C a l l e dw h e nt h eu s e rc l i c k st h eS e n db u t t o n* / p u b l i cv o i ds e n d M e s s a g e ( V i e wv i e w ){ I n t e n ti n t e n t=n e wI n t e n t ( t h i s ,D i s p l a y M e s s a g e A c t i v i t y . c l a s s ) ; E d i t T e x te d i t T e x t=( E d i t T e x t )f i n d V i e w B y I d ( R . i d . e d i t _ m e s s a g e ) ; S t r i n gm e s s a g e=e d i t T e x t . g e t T e x t ( ) . t o S t r i n g ( ) ; i n t e n t . p u t E x t r a ( E X T R A _ M E S S A G E ,m e s s a g e ) ; s t a r t A c t i v i t y ( i n t e n t ) ; }

Now you need to create the D i s p l a y M e s s a g e A c t i v i t y class in order for this to work.

Create the Second Activity

Figure 1. The new activity wizard in Eclipse. To create a new activity using Eclipse: 1. In the window that appears, open the Android folder and select Android Activity. Click Next.
developer.android.com/training/basics/firstapp/starting-activity.html 4/11

10/14/13

Starting Another Activity | Android Developers

2. Select BlankActivity and click Next. 3. Fill in the activity details: Project: MyFirstApp Activity Name: DisplayMessageActivity Layout Name: activity_display_message Title: My Message Hierarchial Parent: com.example.myfirstapp.MainActivity Navigation Type: None Click Finish. If you're using a different IDE or the command line tools, create a new file named D i s p l a y M e s s a g e A c t i v i t y . j a v a in the project's s r c / directory, next to the original M a i n A c t i v i t y . j a v a file. Open the D i s p l a y M e s s a g e A c t i v i t y . j a v a file. If you used Eclipse to create this activity: The class already includes an implementation of the required o n C r e a t e ( ) method. There's also an implementation of the o n C r e a t e O p t i o n s M e n u ( ) method, but you won't need it for this app so you can remove it. There's also an implementation of o n O p t i o n s I t e m S e l e c t e d ( ) which handles the behavior for the action bar's Up behavior. Keep this one the way it is.
2 3 ]APIs are available only on H Because the A c t i o n B a r[ O N E Y C O M B (API level 11) and

higher, you must add a condition around the g e t A c t i o n B a r ( ) method to check the current platform version. Additionally, you must add the @ S u p p r e s s L i n t ( " N e w A p i " ) tag to the o n C r e a t e ( ) method to avoid lint [24] errors. The D i s p l a y M e s s a g e A c t i v i t y class should now look like this:
p u b l i cc l a s sD i s p l a y M e s s a g e A c t i v i t ye x t e n d sA c t i v i t y{ @ S u p p r e s s L i n t ( " N e w A p i " ) @ O v e r r i d e p r o t e c t e dv o i do n C r e a t e ( B u n d l es a v e d I n s t a n c e S t a t e ){ s u p e r . o n C r e a t e ( s a v e d I n s t a n c e S t a t e ) ;
developer.android.com/training/basics/firstapp/starting-activity.html 5/11

10/14/13

Starting Another Activity | Android Developers

s e t C o n t e n t V i e w ( R . l a y o u t . a c t i v i t y _ d i s p l a y _ m e s s a g e ) ; / /M a k es u r ew e ' r er u n n i n go nH o n e y c o m bo rh i g h e rt ou s eA c t i o n B a rA P I s i f( B u i l d . V E R S I O N . S D K _ I N T> =B u i l d . V E R S I O N _ C O D E S . H O N E Y C O M B ){ / /S h o wt h eU pb u t t o ni nt h ea c t i o nb a r . g e t A c t i o n B a r ( ) . s e t D i s p l a y H o m e A s U p E n a b l e d ( t r u e ) ; } } @ O v e r r i d e p u b l i cb o o l e a no n O p t i o n s I t e m S e l e c t e d ( M e n u I t e mi t e m ){ s w i t c h( i t e m . g e t I t e m I d ( ) ){ c a s ea n d r o i d . R . i d . h o m e : N a v U t i l s . n a v i g a t e U p F r o m S a m e T a s k ( t h i s ) ; r e t u r nt r u e ; } r e t u r ns u p e r . o n O p t i o n s I t e m S e l e c t e d ( i t e m ) ; } }

If you used an IDE other than Eclipse, update your D i s p l a y M e s s a g e A c t i v i t y class with the above code.
2 5 ]must implement the o All subclasses of A c t i v i t y[ n C r e a t e ( ) method. The

system calls this when creating a new instance of the activity. This method is where you must define the activity layout with the s e t C o n t e n t V i e w ( ) method and is where you should perform initial setup for the activity components. Note: If you are using an IDE other than Eclipse, your project does not contain the a c t i v i t y _ d i s p l a y _ m e s s a g e layout that's requested by s e t C o n t e n t V i e w ( ) . That's OK because you will update this method later and won't be using that layout.

Add the title string


If you used Eclipse, you can skip to the next section, because the template provides the title string for the new activity. If you're using an IDE other than Eclipse, add the new activity's title to the s t r i n g s . x m l file:
developer.android.com/training/basics/firstapp/starting-activity.html 6/11

10/14/13

Starting Another Activity | Android Developers

< r e s o u r c e s > . . . < s t r i n gn a m e = " t i t l e _ a c t i v i t y _ d i s p l a y _ m e s s a g e " > M yM e s s a g e < / s t r i n g > < / r e s o u r c e s >

Add it to the manifest


All activities must be declared in your manifest file, A n d r o i d M a n i f e s t . x m l , using an
[26] element. < a c t i v i t y >

When you use the Eclipse tools to create the activity, it creates a default entry. If you're using a different IDE, you need to add the manifest entry yourself. It should look like this:
< a p p l i c a t i o n. . .> . . . < a c t i v i t y a n d r o i d : n a m e = " c o m . e x a m p l e . m y f i r s t a p p . D i s p l a y M e s s a g e A c t i v i t y " a n d r o i d : l a b e l = " @ s t r i n g / t i t l e _ a c t i v i t y _ d i s p l a y _ m e s s a g e " a n d r o i d : p a r e n t A c t i v i t y N a m e = " c o m . e x a m p l e . m y f i r s t a p p . M a i n A c t i v i t y "> < m e t a d a t a a n d r o i d : n a m e = " a n d r o i d . s u p p o r t . P A R E N T _ A C T I V I T Y " a n d r o i d : v a l u e = " c o m . e x a m p l e . m y f i r s t a p p . M a i n A c t i v i t y "/ > < / a c t i v i t y > < / a p p l i c a t i o n >

The a n d r o i d : p a r e n t A c t i v i t y N a m e attribute declares the name of this activity's parent activity within the app's logical hierarchy. The system uses this value to implement default navigation behaviors, such as Up navigation [27] on Android 4.1 (API level 16) and higher. You can provide the same navigation behaviors for older versions of Android by using the Support Library [28] and adding the < m e t a [29] element as shown here. d a t a >

Note: Your Android SDK should already include the latest Android Support Library. It's included with the ADT Bundle but if you're using a different IDE, you should have installed it during the Adding Platforms and Packages [30] step. When using the templates in Eclipse, the Support Library is automatically added
developer.android.com/training/basics/firstapp/starting-activity.html 7/11

10/14/13

Starting Another Activity | Android Developers

to your app project (you can see the library's JAR file listed under Android Dependencies). If you're not using Eclipse, you need to manually add the library to your projectfollow the guide for setting up the Support Library [31] then return here. If you're developing with Eclipse, you can run the app now, but not much happens. Clicking the Send button starts the second activity but it uses a default "Hello world" layout provided by the template. You'll soon update the activity to instead display a custom text view, so if you're using a different IDE, don't worry that the app won't yet compile.

Receive the Intent


3 2 ]is invoked by an I 3 3 ] Every A c t i v i t y[ n t e n t[ , regardless of how the user 3 4 ]that started your activity by calling navigated there. You can get the I n t e n t[

g e t I n t e n t ( ) and retrieve the data contained within it.

In the D i s p l a y M e s s a g e A c t i v i t y classs o n C r e a t e ( ) method, get the intent and extract the message delivered by M a i n A c t i v i t y :
I n t e n ti n t e n t=g e t I n t e n t ( ) ; S t r i n gm e s s a g e=i n t e n t . g e t S t r i n g E x t r a ( M a i n A c t i v i t y . E X T R A _ M E S S A G E ) ;

Display the Message


3 5 ]widget and set the To show the message on the screen, create a T e x t V i e w[ 3 6 ]as the root view of the activitys text using s e t T e x t ( ) . Then add the T e x t V i e w[

layout by passing it to s e t C o n t e n t V i e w ( ) . The complete o n C r e a t e ( ) method for D i s p l a y M e s s a g e A c t i v i t y now looks like this:
@ O v e r r i d e p u b l i cv o i do n C r e a t e ( B u n d l es a v e d I n s t a n c e S t a t e ){ s u p e r . o n C r e a t e ( s a v e d I n s t a n c e S t a t e ) ;

developer.android.com/training/basics/firstapp/starting-activity.html

8/11

10/14/13

Starting Another Activity | Android Developers

/ /G e tt h em e s s a g ef r o mt h ei n t e n t I n t e n ti n t e n t=g e t I n t e n t ( ) ; S t r i n gm e s s a g e=i n t e n t . g e t S t r i n g E x t r a ( M a i n A c t i v i t y . E X T R A _ M E S S A G E ) ; / /C r e a t et h et e x tv i e w T e x t V i e wt e x t V i e w=n e wT e x t V i e w ( t h i s ) ; t e x t V i e w . s e t T e x t S i z e ( 4 0 ) ; t e x t V i e w . s e t T e x t ( m e s s a g e ) ; / /S e tt h et e x tv i e wa st h ea c t i v i t yl a y o u t s e t C o n t e n t V i e w ( t e x t V i e w ) ; }

You can now run the app. When it opens, type a message in the text field, click Send, and the message appears on the second activity.

Figure 2. Both activities in the final app, running on Android 4.0. That's it, you've built your first Android app! To learn more, follow the link below to the next class.

developer.android.com/training/basics/firstapp/starting-activity.html

9/11

10/14/13

Starting Another Activity | Android Developers

1. http://developer.android.com/training/basics/firstapp/building-ui.html 2. http://developer.android.com/reference/android/widget/Button.html 3. http://developer.android.com/reference/android/view/View.html 4. http://developer.android.com/reference/android/view/View.html 5. http://developer.android.com/reference/android/view/View.html 6. http://developer.android.com/reference/android/content/Intent.html 7. http://developer.android.com/reference/android/content/Intent.html 8. http://developer.android.com/reference/android/content/Intent.html 9. http://developer.android.com/reference/android/content/Context.html 10. http://developer.android.com/reference/android/app/Activity.html 11. http://developer.android.com/reference/android/content/Context.html 12. http://developer.android.com/reference/java/lang/Class.html 13. http://developer.android.com/reference/android/content/Intent.html 14. http://developer.android.com/reference/android/content/Intent.html 15. http://developer.android.com/reference/android/content/Intent.html 16. http://developer.android.com/reference/android/content/Intent.html 17. http://developer.android.com/training/basics/intents/index.html 18. http://developer.android.com/reference/android/widget/EditText.html 19. http://developer.android.com/reference/android/content/Intent.html 20. http://developer.android.com/reference/android/content/Intent.html 21. http://developer.android.com/reference/android/app/Activity.html 22. http://developer.android.com/reference/android/content/Intent.html 23. http://developer.android.com/reference/android/app/ActionBar.html 24. http://developer.android.com/tools/help/lint.html 25. http://developer.android.com/reference/android/app/Activity.html 26. http://developer.android.com/guide/topics/manifest/activity-element.html 27. http://developer.android.com/design/patterns/navigation.html 28. http://developer.android.com/tools/support-library/index.html
developer.android.com/training/basics/firstapp/starting-activity.html 10/11

10/14/13

Starting Another Activity | Android Developers

29. http://developer.android.com/guide/topics/manifest/meta-data-element.html 30. http://developer.android.com/sdk/installing/adding-packages.html 31. http://developer.android.com/tools/support-library/setup.html 32. http://developer.android.com/reference/android/app/Activity.html 33. http://developer.android.com/reference/android/content/Intent.html 34. http://developer.android.com/reference/android/content/Intent.html 35. http://developer.android.com/reference/android/widget/TextView.html 36. http://developer.android.com/reference/android/widget/TextView.html

developer.android.com/training/basics/firstapp/starting-activity.html

11/11