In Android, an Activity can be understood as a screen. Intent is used to switch a program from one Activity to another.
Intent is responsible for interaction and communication between Android programs and between activities and services in the program.
The following is a simple demo, which uses Intent to switch from one Activity to another, and transmits a data for display.
Take a look --
First Activity:
Enter the name "Barry ":
Click "OK" to jump to another Activity and display "Hello, Barry !" :
The Code is as follows:
Demo_intentActivity.java
[Java]
Package barry. android. intent;
Import android. app. Activity;
Import android. content. Intent;
Import android. OS. Bundle;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Import android. widget. EditText;
Public class Demo_intentActivity extends Activity {
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Button btn = (Button) findViewById (R. id. button1 );
Btn. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v ){
String yourName = (EditText) findViewById (R. id. name_text ))
. GetText (). toString ();
Intent intent = new Intent ();
Intent. setClass (Demo_intentActivity.this, RecordActivity. class );
Intent. putExtra ("name", yourName); // set the data to be passed in Intent
StartActivity (intent); // start a new Activity
Demo_intentActivity.this.finish (); // end the Activity.
}
});
}
}
Package barry. android. intent;
Import android. app. Activity;
Import android. content. Intent;
Import android. OS. Bundle;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Import android. widget. EditText;
Public class Demo_intentActivity extends Activity {
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Button btn = (Button) findViewById (R. id. button1 );
Btn. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v ){
String yourName = (EditText) findViewById (R. id. name_text ))
. GetText (). toString ();
Intent intent = new Intent ();
Intent. setClass (Demo_intentActivity.this, RecordActivity. class );
Intent. putExtra ("name", yourName); // set the data to be passed in Intent
StartActivity (intent); // start a new Activity
Demo_intentActivity.this.finish (); // end the Activity.
}
});
}
} And its layout file main. xml
[Html]
<? 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">
<EditText
Android: id = "@ + id/name_text"
Android: hint = "enter your name"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
/>
<Button android: text = "OK"
Android: id = "@ + id/button1"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"/>
</LinearLayout>
<? 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">
<EditText
Android: id = "@ + id/name_text"
Android: hint = "enter your name"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
/>
<Button android: text = "OK"
Android: id = "@ + id/button1"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"/>
</LinearLayout>
Another Activity RecordActivity. java
[Java]
Package barry. android. intent;
Import android. app. Activity;
Import android. content. Intent;
Import android. OS. Bundle;
Import android. widget. TextView;
Public class RecordActivity extends Activity {
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. record );
Intent intent = getIntent ();
String yourname = intent. getStringExtra ("name"); // get data from Intent
TextView text_show = (TextView) findViewById (R. id. text_show );
Text_show.setText ("hello," + yourname + "! "); // Display to the screen
}
}
Package barry. android. intent;
Import android. app. Activity;
Import android. content. Intent;
Import android. OS. Bundle;
Import android. widget. TextView;
Public class RecordActivity extends Activity {
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. record );
Intent intent = getIntent ();
String yourname = intent. getStringExtra ("name"); // get data from Intent
TextView text_show = (TextView) findViewById (R. id. text_show );
Text_show.setText ("hello," + yourname + "! "); // Display to the screen
}
}
And its layout file record. xml
[Html]
<? 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: id = "@ + id/text_show"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"/>
</LinearLayout>
<? 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: id = "@ + id/text_show"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"/>
</LinearLayout>
Note that each Activity must be declared in AndroidManiFest. xml. Therefore, you must add the following to AndroidManiFest. xml:
[Html]
<Activity android: name = ". RecordActivity"> </activity>
From Wolf's second nest