[Android Studio] Section 5 Intent page jump

Table of contents

Table of contents

1. What is Intent?

2. Usage steps

1. Reverse demo

2.Detailed explanation

3. Page data transfer


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:

  1. 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, contextis the context of the current activity, and TargetActivity.classis 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.
  2. 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.
  3. Launching a component: The most common usage is to startActivity()launch an activity by calling a method. In addition, you can also use to startService()start services, sendBroadcast()send broadcasts, etc.

  4. 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 as getXXXExtra()) 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 EnglishActivitystarts an DictionaryTableActivityactivity named within another activity named. The following is an analysis of this code:

  1. 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.
  2. Start activity: startActivity(intent);This line of code calls startActivity()the method, passing in the created Intent object as a parameter, and starts the target activity.

With the above code, when EnglishActivityexecuted in , it creates an intent (Intent) with the current activity as the context and defines the target activity as and DictionaryTableActivitythen starts the activity. This will cause a jump to DictionaryTableActivityand 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 EnglishActivitythe 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);
    }
}

DictionaryTableActivityUse 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".

Guess you like

Origin blog.csdn.net/AA2534193348/article/details/131445051