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