An Android app can contain zero or multiple acivity values. When your application contains multiple activities, you usually need to jump between them. In Android, intent components are required to complete these operations.
The best way to understand this important and abstract concept is to try it. The following example shows how to jump between two activities.
1. Create a project, usingintent.
2. Create two activities: usingintentactivity and secondactivitty.
3. The code in androidmanifest. xml.
[Java]View plaincopy
- <? XML version = "1.0" encoding = "UTF-8"?>
- <Manifest xmlns: Android = "http://schemas.android.com/apk/res/android"
- Package = "net. horsttnann. usingintent"
- Android: versioncode = "1"
- Android: versionname = "1.0" type = "codeph" text = "/codeph">
- <Uses-SDK Android: minsdkversion = "14"/>
- <Application
- Android: icon = "@ drawable/ic_launcher"
- Android: Label = "@ string/app_name">
- <Activity
- Android: Name = ". usingintentactivity"
- 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 = ". secondactivity"
- Android: Label = "second activity">
- <Intent-filter>
- <Action Android: Name = "net. horsttnann. secondactivity"/>
- <Category Android: Name = "android. Intent. Category. Default"/>
- </Intent-filter>
- </Activity>
- </Application>
- </Manifest>
4. In the Res/layout folder, create a file named secondactivity. xml.[Java]View plaincopy
- <? 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! "/>
- <Textview
- Android: layout_width = "fill_parent"
- Android: layout_height = "wrap_content"
- Android: text = "Please enter your name"/>
- <Edittext
- Android: Id = "@ + ID/txt_username"
- Android: layout_width = "fill_parent"
- Android: layout_height = "wrap_content"/>
- <Button
- Android: Id = "@ + ID/btn_ OK"
- Android: layout_width = "fill_parent"
- Android: layout_height = "wrap_content"
- Android: onclick = "onclick"
- Android: text = "OK"/>
- </Linearlayout>
5. Code in secondactivity.[Java]View plaincopy
- Public class secondactivity extends activity {
- @ Override
- Public void oncreate (bundle savedinstancestate ){
- Super. oncreate (savedinstancestate );
- Setcontentview (R. layout. secondactivity );
- }
- }
6. Code in Main. xml.[Java]View plaincopy
- <? 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: onclick = "onclick"
- Android: text = "display second activity"/>
- </Linearlayout>
7. The code in usingintentactivity.[Java]View plaincopy
- 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 ){
- Startactivity (new intent ("net. horsttnann. secondactivity "));
- }
- }
8. debug by F11.
Use Reverse Domain Names as much as possible for the anction attribute in intent-filter, which can reduce the chances of being started by other programs that use the same action.
The category attribute in intent-filter is Android. Intent. Category. default. Only when this attribute is added can this activity be enabled by another activity using the startactivity () method.
There is another method to start the activity, but if you want to use this method, you must ensure that the two activities are under the same project.
[Java]View plaincopy
- Startactivity (new intent (this, secondactivity. Class ));