Android studio core activity-intent
task
Create a new project on Android studio, and
显示Intent
open another activity in one activity with three types . And练习采用隐示Intent
open 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
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.
Resource file. The
program configuration file
uses @string to configure hello world.
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;
customize the name;
click OK and the interface;
bind in mainactivity;
add a button;
run, view the result;
Two, intent entry
1. Start another activity
Define a new activity; 选择basic activity
define the interface name;
as shown
in the figure after creation; set a button in the layout of mainActivity to start the acitvity, set the button ID;
set the ID
code as shown in the figure;
there is corresponding configuration information on the left;
2. Communication between the two components
activity stack
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;
Method 2: Bind an onClick event directly to the control , Create the corresponding public method in MainActivity;
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
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;
according to whether the activity to be opened is the main interface, configure export=true in AndroidManifest.xml;
- 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;
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 .