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 named UsingIntent.
 
2. Create two activities: UsingIntentActivity and SecondActivitty.
 
3. The code in AndroidManifest. xml.
 
<? 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. <? 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. Package net. horsttnann. 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 );
}
}
6. Code in main. xml. <? 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. Package net. horsttnann. 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. horsttnann. SecondActivity "));
}
 
}
8. debug by F11. Www.2cto.com
 
 
 
:
 


Tip:
 
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.
 
 
StartActivity (new Intent (this, SecondActivity. class ));
 
 
Written by manoel

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.