Table of contents
Table of contents
1. What is Intent?
Intent is an object used in Android to pass data and perform operations between different components (such as activities, services, broadcast receivers). It can be used to launch activities, start services, send broadcasts, and perform various other operations. The following is a specific analysis of Intent:
-
Intent construction: Intent objects can be created through different construction methods to meet different usage needs. For example:
Intent intent = new Intent(context, TargetActivity.class);
Create an Intent object that explicitly specifies the target activity,context
is the context of the current activity, andTargetActivity.class
is the class of the target activity.Intent intent = new Intent(action);
Create an Intent object that specifies the operation behavior. For example, you can use this constructor when sending a broadcast.
-
Set Intent properties: The Intent object has many settable properties, commonly used ones include:
- Target component: Specify which component the Intent wants to start or pass data to.
- Data passing:
putExtra()
Add additional data to the Intent through methods and store it using key-value pairs. - Action and category: Set the action and category of the Intent through
setAction()
the and methods.addCategory()
- Flags:
setFlags()
Set the flag bits of Intent through methods, such as FLAG_ACTIVITY_NEW_TASK, FLAG_ACTIVITY_CLEAR_TOP, etc.
-
Launching a component: The most common usage is to
startActivity()
launch an activity by calling a method. In addition, you can also use tostartService()
start services,sendBroadcast()
send broadcasts, etc. -
Data passing and receiving: Intents can be used to pass data between different components. When sending an Intent, you can use
putExtra()
methods to add data to the Intent; when receiving an Intent, you can use the corresponding methods (such asgetXXXExtra()
) to obtain the passed data.
Intent needs to be used according to specific situations and different attributes set according to needs. Through Intent, communication and interaction between different components can be achieved.
2. Usage steps
1. Reverse demo
Intent intent = new Intent(EnglishActivity.this,DictionaryTableActivity.class);
startActivity(intent);
2.Detailed explanation
This code EnglishActivity
starts an DictionaryTableActivity
activity named within another activity named. The following is an analysis of this code:
-
Create an Intent object:
Intent intent = new Intent(EnglishActivity.this, DictionaryTableActivity.class);
This line of code creates a new Intent object and specifies two parameters:EnglishActivity.this
: This is a context parameter that specifies the context of the current activity and is used to distinguish different activities.DictionaryTableActivity.class
: This is the Class object pointing to the target activity, specifying the activity to be started.
-
Start activity:
startActivity(intent);
This line of code callsstartActivity()
the method, passing in the created Intent object as a parameter, and starts the target activity.
With the above code, when EnglishActivity
executed in , it creates an intent (Intent) with the current activity as the context and defines the target activity as and DictionaryTableActivity
then starts the activity. This will cause a jump to DictionaryTableActivity
and display of the activity's interface.
3. Page data transfer
Intent intent = new Intent();
intent.setClass(EnglishActivity.this,DictionaryTableActivity.class);
intent.putExtra("extra_data","Ni hao");
startActivity(intent);
This code creates a new Intent object and sets the target activity to be launched by the Intent DictionaryTableActivity
. Then, use putExtra()
the method to add an additional data called "extra_data" to the Intent and set the value to "Ni hao".
Finally, startActivity()
the Intent is passed to EnglishActivity
the context of the current activity ( ) by calling a method to start the target activity DictionaryTableActivity
.
public class DictionaryTableActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dictionary_table);
Intent intent = getIntent();
String data = intent.getStringExtra("extra_data");
System.out.println(data);
}
}
DictionaryTableActivity
Use the method in to getIntent()
get the passed Intent, and use getStringExtra("extra_data")
the method to get the value corresponding to the "extra_data" key, which is "Ni hao".