Android studio core activity-intent

task

Create a new project on Android studio, and 显示Intentopen another activity in one activity with three types . And 练习采用隐示Intentopen an Activity.

1. Getting started with activity

1. What is activity?

Activity is the four most basic and common components of Android components.

(Activity,Service,Content Provider,BroadcastReceiver)

Simply put, an activity is an interactive interface that covers the entire window or floats on other windows. An application usually consists of multiple activities. Each activity will configure the corresponding node in Androidmanifest.xml, the content of the node is as follows; the
Insert picture description here
code is as follows:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.helloworld">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="world"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.HelloWorld">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Create a new project. Look at the relationship between MainAtivity.java and layout and AndroidMainfest.xml, as well as the structure of the project file.
Insert picture description here
Resource file. The
Insert picture description here
program configuration file
Insert picture description here
uses @string to configure hello world.
Insert picture description here
Insert picture description here

2, activity binding custom view (layout)

  • View binding in oncreate in mainactivity;
  • Bind a custom view.
    Create a new my_activity view, and bind it in the surface activity;
    Insert picture description here
    customize the name;
    Insert picture description here
    click OK and the interface;
    Insert picture description here
    bind in mainactivity;
    Insert picture description here
    add a button;
    Insert picture description here
    run, view the result;
    Insert picture description here

Two, intent entry

1. Start another activity

Define a new activity; 选择basic activity
Insert picture description here
define the interface name;
Insert picture description here
as shown
Insert picture description here
in the figure after creation; set a button in the layout of mainActivity to start the acitvity, set the button ID;
Insert picture description here
set the ID
Insert picture description here
code as shown in the figure;
Insert picture description here
there is corresponding configuration information on the left;
Insert picture description here

2. Communication between the two components

activity stack
Insert picture description here
Insert picture description here
intent (intent)

Android provides an Intent mechanism to assist in the interaction and communication between applications, or to adopt a more accurate statement, Intent can be used not only between applications, but also for interactions between activities, services, and broadcast receivers within applications.
The original meaning of the English word Intent is "purpose, intent, intent". Intent is a runtime binding mechanism, which can connect two different components during the running of the program. Through Intent, your program can express a certain request or willingness to Android, and Android will select the appropriate component to respond according to the content of the willingness.

Intent related attributes

  • component (component): destination component;
  • action: an action used to express intent;
  • category: the category used to represent the action;
  • data (data): indicates the data to be manipulated with the action;
  • type (data type): description of the data example
  • extras (extended information): extended information

The code in mainActivtiy.java
binds a click event to the Android control;
Method 1: Obtain the control by id, and use the anonymous internal class method to bind the listener;
Insert picture description here
Method 2: Bind an onClick event directly to the control , Create the corresponding public method in MainActivity;
Insert picture description here
I didn’t test it here. You can try the above method as the tutorial picture is pasted.

3. Intent (intent)

Intent can be divided into display intent and implicit intent.

3.1 Display intent

Display Intent: For the Intent that clearly indicates the component name, we call it the display intent

Three display intents
Insert picture description here

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private Button btn_start1;
    private Button btn_start2;
    private Button btn_start3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_start1 = (Button) findViewById(R.id.btn_start1);
        btn_start2 = (Button) findViewById(R.id.btn_start2);
        btn_start3 = (Button) findViewById(R.id.btn_start3);
        btn_start1.setOnClickListener(this);
        btn_start2.setOnClickListener(this);
        btn_start3.setOnClickListener(this);

Start the main interface of an application;
Insert picture description here
according to whether the activity to be opened is the main interface, configure export=true in AndroidManifest.xml;
Insert picture description here

  • In Activity, this attribute is used to indicate: whether the current Activity can be started by another Application component: true is allowed to be started; false is not allowed to be started.
  • The main interface defaults to true, and other activities default to false. For example, the list can open other applications. And uses the implicit schematic method

3.2 Implicit intent

In the OtherActivity opened in the previous example, an implicit Intent is used to open another activity;
Insert picture description here
Insert picture description here

Three, summary and reference materials

1. Summary

Have a deeper understanding of the activity interface, and know how to switch the interface. In the process, I also had many things I didn't understand, basically I followed the tutorial to do it, and then Baidu solved the things I didn't understand. Work together and make progress together!

2. Reference materials

Activity binds a custom view .
The activity must be exported or contain an intent-filter .

Guess you like

Origin blog.csdn.net/QWERTYzxw/article/details/114787189