Android Program Development: (2) Intention of use-2.1 link activities

Source: Internet
Author: User

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
  1. <? XML version = "1.0" encoding = "UTF-8"?>
  2. <Manifest xmlns: Android = "http://schemas.android.com/apk/res/android"
  3. Package = "net. horsttnann. usingintent"
  4. Android: versioncode = "1"
  5. Android: versionname = "1.0" type = "codeph" text = "/codeph">
  6. <Uses-SDK Android: minsdkversion = "14"/>
  7. <Application
  8. Android: icon = "@ drawable/ic_launcher"
  9. Android: Label = "@ string/app_name">
  10. <Activity
  11. Android: Name = ". usingintentactivity"
  12. Android: Label = "@ string/app_name">
  13. <Intent-filter>
  14. <Action Android: Name = "android. Intent. Action. Main"/>
  15. <Category Android: Name = "android. Intent. Category. launcher"/>
  16. </Intent-filter>
  17. </Activity>
  18. <Activity
  19. Android: Name = ". secondactivity"
  20. Android: Label = "second activity">
  21. <Intent-filter>
  22. <Action Android: Name = "net. horsttnann. secondactivity"/>
  23. <Category Android: Name = "android. Intent. Category. Default"/>
  24. </Intent-filter>
  25. </Activity>
  26. </Application>
  27. </Manifest>

4. In the Res/layout folder, create a file named secondactivity. xml.[Java]View plaincopy

  1. <? XML version = "1.0" encoding = "UTF-8"?>
  2. <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
  3. Android: layout_width = "fill_parent"
  4. Android: layout_height = "fill_parent"
  5. Android: Orientation = "vertical">
  6. <Textview
  7. Android: layout_width = "fill_parent"
  8. Android: layout_height = "wrap_content"
  9. Android: text = "this is the second activity! "/>
  10. <Textview
  11. Android: layout_width = "fill_parent"
  12. Android: layout_height = "wrap_content"
  13. Android: text = "Please enter your name"/>
  14. <Edittext
  15. Android: Id = "@ + ID/txt_username"
  16. Android: layout_width = "fill_parent"
  17. Android: layout_height = "wrap_content"/>
  18. <Button
  19. Android: Id = "@ + ID/btn_ OK"
  20. Android: layout_width = "fill_parent"
  21. Android: layout_height = "wrap_content"
  22. Android: onclick = "onclick"
  23. Android: text = "OK"/>
  24. </Linearlayout>

5. Code in secondactivity.[Java]View plaincopy

  1. Public class secondactivity extends activity {
  2. @ Override
  3. Public void oncreate (bundle savedinstancestate ){
  4. Super. oncreate (savedinstancestate );
  5. Setcontentview (R. layout. secondactivity );
  6. }
  7. }

6. Code in Main. xml.[Java]View plaincopy

  1. <? XML version = "1.0" encoding = "UTF-8"?>
  2. <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
  3. Android: layout_width = "fill_parent"
  4. Android: layout_height = "fill_parent"
  5. Android: Orientation = "vertical">
  6. <Button
  7. Android: layout_width = "fill_parent"
  8. Android: layout_height = "wrap_content"
  9. Android: onclick = "onclick"
  10. Android: text = "display second activity"/>
  11. </Linearlayout>

7. The code in usingintentactivity.[Java]View plaincopy

  1. Public class usingintentactivity extends activity {
  2. /** Called when the activity is first created .*/
  3. @ Override
  4. Public void oncreate (bundle savedinstancestate ){
  5. Super. oncreate (savedinstancestate );
  6. Setcontentview (R. layout. Main );
  7. }
  8. Public void onclick (view ){
  9. Startactivity (new intent ("net. horsttnann. secondactivity "));
  10. }
  11. }

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
  1. Startactivity (new intent (this, secondactivity. Class ));

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.