Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the use of Activity in Android

2025-04-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you what is the use of Activity in Android, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

A theoretical overview of the understanding of Activity the definition of Activity

Activity, literally translated as activity, is one of the four application components defined by Android, and it is also the most important and most widely used.

An application has multiple interfaces, that is, it contains multiple Activity

The role of Activity

Used to provide an interface that allows users to operate and interact with it

An Activity is equivalent to a container for storing various controls, and it can also be understood as an interface to interact with the user.

Analogy between Activity and Servlet

The understanding of Intent

Intent, literally translated as intention, that is, what you want to do or where you want to go

Intent (the messenger of communication between Activity, Service and BroadcastReceiver), but it is not one of the four major components in Android.

For example, if I want to start another Activity in Activity, I must use the Intent object. Intent objects can also carry data

Classification of Intent

Explicit: specify the intention of the specified target component (that is, know exactly what to do or where to go), and use explicit Intent when manipulating your own components

Implicit: do not explicitly specify the intention of the target component (that is, you don't know exactly what to do or where to go), and use implicit Intent when manipulating your own components.

The use of Intent

Create

Explicit: Intent (Context context, Class activityClass), Intent objects created with such constructors are explicit

Implicit: Intent (String action) / / matches the action of Activity and Intent. The Intent object created using such a constructor is implicit. For example, the sharing function also starts another Activity, but which Activity is launched is unknown and is chosen by the user.

Carry data

Extra: putExtra (String key, Xxx value) is stored inside a map container

There is a specific prefix: setData (Uri data) / / tel:123123, smsto:123123

Read data

Extra: Xxx getXxxExtra (String key)

There is a specific prefix: Uri getData ()

The understanding of IntentFilter

When configuring Activity, you can specify a configuration of IntentFilter for Activity

If your Activity wants other applications to access it, you need to configure

If the Activity is accessed by some Activity of the internal application, there is no need to configure

If you want to launch other application interfaces, you must use implicit intent, and the target interface Activity is configured

Click to add a listening view.setOnClickListener (OnClickListener listener) to the listening Activity and add listening to the layout in the relevant API settings

In layout

In Activity

Package com.example.activity_01; import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.Toast; public class MainActivity extends Activity {@ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main);} public void MainStart2 (View view) {Toast.makeText (this, start with callback, Toast.LENGTH_SHORT) .show () }} set long press monitor view.setOnLongClickListener (OnLongClickListener listener)

Activity development process 1. Interface layout 2. Realize the function of Activity 1). Define all view objects that need to be operated and initialize 2). Set monitoring for the view 3). Implement logic 3 in the callback method. Implementation generally starts 1). Define interface 2. 1). Layout 2). Define Activity class 3). Configuration 4). Override onCreate () and load layout 2). Start interface 2 1). Create an Intent object (explicit) 2). Carrying additional data via intent 3). Start Activity 4). Get intent object 5). Read extra data through intent 6). Display to EditText4. The implementation generally returns: 1). When displaying the Second interface, the Main interface is really, just covered 2). Close the current interface: the use of finish () Activity

Interface layout

Realize the function of Activity

1)。 Define all view objects that need to be operated and initialize

2)。 Set up monitoring for the view

3)。 Implement logic in callback method

Package com.example.activity; import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.Toast; import com.google.android.material.textfield.TextInputEditText; public class MainActivity extends Activity implements View.OnClickListener {private TextInputEditText main_message; private Button btn_main_start1; private Button btn_main_start2; @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState) SetContentView (R.layout.activity_main); / / View object and initialize main_message = findViewById (R.id.et_main_message); btn_main_start1 = findViewById (R.id.btn_main_start1); btn_main_start2 = findViewById (R.id.btn_main_start2); / / set listening btn_main_start1.setOnClickListener (this) for view Btn_main_start2.setOnClickListener (this);} / / implement logic @ Override public void onClick (View view) {/ / view in the callback method is the view object if (view = = btn_main_start1) {Toast.makeText (this, "General launch", Toast.LENGTH_SHORT). Show () } else if (view = = btn_main_start2) {Toast.makeText (this, "start with callback", Toast.LENGTH_SHORT) .show ();}

Achieve general startup

1)。 Define interface two.

1)。 Overall Arrangement

2)。 Define the Activity class

3)。 Configuration

4)。 Override onCreate () and load the layout

2)。 Start interface two

1)。 Create an Intent object (explicit)

2)。 Carrying extra data through intent

3)。 Start Activity

4)。 Get the intent object

5)。 Read extra data through intent

6)。 Show to TextInputEditText

Configuration list

The above is all the content of this article "what is the use of Activity in Android?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report