Intent, also known as intent, is a runtime binding mechanism that can link two different components (Activity, Service, BroadcastReceiver) during the process of running the program. Through Intent, the program can express a certain request or intention to Android, and Android will select the appropriate component to request it according to the content of the wish.
In the communication between these components, it is mainly done with the assistance of Intent. The Intent is responsible for describing the actions, data involved in the action and additional data of an operation in the application. Android is responsible for finding the corresponding components based on the description of this Intent, passing the Intent to the calling component, and completing the component's call. Therefore, Intent plays a mediator here, specifically providing relevant information about components calling each other, realizing the decoupling between the caller and the callee.
Requesting Activity through Intent, you must add tag configuration to the requested Activity in the file, otherwise an error will be caused.
Intent generally contains two main information, action and data.
action: Indicates the action of this Intent.
data: Indicates the data involved in this action.
Use an example to demonstrate the use of Intent in Activity to direct new Activity and pass data. This program only jumps between two pages, but each jump will create a new activity. Therefore, after startActivity(), you need to call finish() to destroy the current activity. If it is not destroyed, after multiple jumps, multiple activities will be stored in the program's Activity stack. Click the device's return button and you will find that it will keep going backwards.
Main steps:
Create a new Android project, add a new layout file, and add an Activity class to accept Intent and display it.
In the MainActivity class, declare an Intent class and specify the source and destination through the Intent constructor.
After obtaining the Intent, use the() method to pass in the data.
Call to start this Intent.
In otherActivity class, use() to get the Intent of the current Activity.
After obtaining the Intent, use the () method to obtain the saved data.
Configure otherActivity node.
Sample code
Step 2-3:
public class MainActivity extends Activity {
private TextView textView;
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
(savedInstanceState);
setContentView(.activity_main);
textView=(TextView)findViewById(.textView1);
btn=(Button)findViewById(.button1);
(new () {
@Override
public void onClick(View v) {
// Intent constructor: Intent source; Intent purpose.
Intent intent =new Intent(,);
("data", "currently page 2, information comes from page 1");
startActivity(intent);//Start Activity
finish();
}
});
}
}
Step 4-5:
public class otherActivity extends Activity {
private Button btn;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
(savedInstanceState);
setContentView();
textView=(TextView)findViewById(.textView2);
btn=(Button)findViewById(.button2);
//Get the Intent received by the current page through ().
Intent intent =getIntent();
//getXxxExtra method gets the data passed by Intent
String msg=("data");
(msg);
(new () {
@Override
public void onClick(View v) {
Intent intent=new Intent(,);
startActivity(intent);
finish();
}
});
}
}
Step 7:
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=""
android:label="@string/app_name" >
<intent-filter>
<action android:name="" />
<category android:name="" />
</intent-filter>
</activity>
<activity android:name=".otherActivity"/>
</application>
Return data from Activity
In the above example, the Activity only introduces the transfer of data through Intent. However, in actual applications, it is not only necessary to pass data to the Activity, but also to return data from the Activity. Although returning data is similar to passing data, there are still some differences.
The main differences are as follows:
Passing data requires the activity to be started using the () method, and the request code needs to be passed, not ().
When returning data, call the () method to set the return Intent and return code.
The onActivityResult() method of the source Activity needs to be rewrite to facilitate the acceptance of the returned Intent, and the request code and response code will be judged in onActivityResult().
Use an example to illustrate returning data from an Activity. This program has two activities. Enter the calculation number of the addition operation in MainActivity, jump to otherActivity to enter the calculation result, and after clicking to return, output the calculation result to MainActivity.
Sample code
MainActivity:
public class MainActivity extends Activity {
private EditText one,two,result;
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
(savedInstanceState);
setContentView(.activity_main);
one=(EditText)findViewById();
two=(EditText)findViewById();
result=(EditText)findViewById();
btn=(Button)findViewById();
(new () {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int ione=(().toString());
int itwo=(().toString());
Intent intent=new Intent(, );
("one", ione);
("two", itwo);
//Start the activity that needs to listen for the return value and set the request code: requestCode
startActivityForResult(intent, 1);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
(requestCode, resultCode, data);
//When data is returned in otherActivity, this method will be responded to
//RequestCode and resultCode must be consistent with the values passed in when requesting startActivityForResult() and returning setResult().
if(requestCode==1&&resultCode==2)
{
int three=("three", 0);
((three));
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(, menu);
return true;
}
}
otherActivity:
public class otherActivity extends Activity {
TextView tvShow;
EditText etResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
(savedInstanceState);
setContentView();
tvShow=(TextView)findViewById();
etResult=(EditText)findViewById();
Intent intent=getIntent();
int a=("one", 0);
int b=("two", 0);
(a+" + "+b+" = "+" ? ");
Button btnResult=(Button)findViewById();
(new () {
@Override
public void onClick(View v) {
//A new declaration of an Intent is used to store the data that is put back
Intent i=new Intent();
int result=(().toString());
("three", result);
setResult(2, i);//Set resultCode, onActivityResult() can be obtained
finish();//End the life cycle of the current activity after use
}
});
}
}
In the communication between these components, it is mainly done with the assistance of Intent. The Intent is responsible for describing the actions, data involved in the action and additional data of an operation in the application. Android is responsible for finding the corresponding components based on the description of this Intent, passing the Intent to the calling component, and completing the component's call. Therefore, Intent plays a mediator here, specifically providing relevant information about components calling each other, realizing the decoupling between the caller and the callee.
Requesting Activity through Intent, you must add tag configuration to the requested Activity in the file, otherwise an error will be caused.
Intent generally contains two main information, action and data.
action: Indicates the action of this Intent.
data: Indicates the data involved in this action.
Use an example to demonstrate the use of Intent in Activity to direct new Activity and pass data. This program only jumps between two pages, but each jump will create a new activity. Therefore, after startActivity(), you need to call finish() to destroy the current activity. If it is not destroyed, after multiple jumps, multiple activities will be stored in the program's Activity stack. Click the device's return button and you will find that it will keep going backwards.
Main steps:
Create a new Android project, add a new layout file, and add an Activity class to accept Intent and display it.
In the MainActivity class, declare an Intent class and specify the source and destination through the Intent constructor.
After obtaining the Intent, use the() method to pass in the data.
Call to start this Intent.
In otherActivity class, use() to get the Intent of the current Activity.
After obtaining the Intent, use the () method to obtain the saved data.
Configure otherActivity node.
Sample code
Step 2-3:
Copy the codeThe code is as follows:
public class MainActivity extends Activity {
private TextView textView;
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
(savedInstanceState);
setContentView(.activity_main);
textView=(TextView)findViewById(.textView1);
btn=(Button)findViewById(.button1);
(new () {
@Override
public void onClick(View v) {
// Intent constructor: Intent source; Intent purpose.
Intent intent =new Intent(,);
("data", "currently page 2, information comes from page 1");
startActivity(intent);//Start Activity
finish();
}
});
}
}
Step 4-5:
Copy the codeThe code is as follows:
public class otherActivity extends Activity {
private Button btn;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
(savedInstanceState);
setContentView();
textView=(TextView)findViewById(.textView2);
btn=(Button)findViewById(.button2);
//Get the Intent received by the current page through ().
Intent intent =getIntent();
//getXxxExtra method gets the data passed by Intent
String msg=("data");
(msg);
(new () {
@Override
public void onClick(View v) {
Intent intent=new Intent(,);
startActivity(intent);
finish();
}
});
}
}
Step 7:
Copy the codeThe code is as follows:
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=""
android:label="@string/app_name" >
<intent-filter>
<action android:name="" />
<category android:name="" />
</intent-filter>
</activity>
<activity android:name=".otherActivity"/>
</application>
Return data from Activity
In the above example, the Activity only introduces the transfer of data through Intent. However, in actual applications, it is not only necessary to pass data to the Activity, but also to return data from the Activity. Although returning data is similar to passing data, there are still some differences.
The main differences are as follows:
Passing data requires the activity to be started using the () method, and the request code needs to be passed, not ().
When returning data, call the () method to set the return Intent and return code.
The onActivityResult() method of the source Activity needs to be rewrite to facilitate the acceptance of the returned Intent, and the request code and response code will be judged in onActivityResult().
Use an example to illustrate returning data from an Activity. This program has two activities. Enter the calculation number of the addition operation in MainActivity, jump to otherActivity to enter the calculation result, and after clicking to return, output the calculation result to MainActivity.
Sample code
MainActivity:
Copy the codeThe code is as follows:
public class MainActivity extends Activity {
private EditText one,two,result;
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
(savedInstanceState);
setContentView(.activity_main);
one=(EditText)findViewById();
two=(EditText)findViewById();
result=(EditText)findViewById();
btn=(Button)findViewById();
(new () {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int ione=(().toString());
int itwo=(().toString());
Intent intent=new Intent(, );
("one", ione);
("two", itwo);
//Start the activity that needs to listen for the return value and set the request code: requestCode
startActivityForResult(intent, 1);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
(requestCode, resultCode, data);
//When data is returned in otherActivity, this method will be responded to
//RequestCode and resultCode must be consistent with the values passed in when requesting startActivityForResult() and returning setResult().
if(requestCode==1&&resultCode==2)
{
int three=("three", 0);
((three));
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(, menu);
return true;
}
}
otherActivity:
Copy the codeThe code is as follows:
public class otherActivity extends Activity {
TextView tvShow;
EditText etResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
(savedInstanceState);
setContentView();
tvShow=(TextView)findViewById();
etResult=(EditText)findViewById();
Intent intent=getIntent();
int a=("one", 0);
int b=("two", 0);
(a+" + "+b+" = "+" ? ");
Button btnResult=(Button)findViewById();
(new () {
@Override
public void onClick(View v) {
//A new declaration of an Intent is used to store the data that is put back
Intent i=new Intent();
int result=(().toString());
("three", result);
setResult(2, i);//Set resultCode, onActivityResult() can be obtained
finish();//End the life cycle of the current activity after use
}
});
}
}