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

How to use Activity components in Android

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Today, I would like to share with you the relevant knowledge points about how to use the Activity components in Android. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article.

Overview of Activity

In Android applications, four basic components are provided, namely, Activity, Service, BroadcastReceiver and ContentProvider. Activity is one of the most common components of Android applications. Activity means activity in Chinese. In Android, Activity represents a screen in a phone or tablet that provides a visual interface to interact with the user. In an Activity, you can add a number of components that are responsible for specific functions.

In an Android application, there can be multiple Activity. These Activity make up the Activity stack (Stack). The currently active Activity is at the top of the stack, and the previous Activity is pressed below to become an inactive Activity, waiting for whether it is possible to return to the active state.

Two cases of starting Activity

①, when there is only one Activity in an Android application, you only need to comment it in the AndroidManifest.xml file and set it as the entry of the program. In this way, when running, the Activity will be started automatically.

②, when there is more than one Activity in an Android application, you need to apply the startActivity () method to start the required Activity.

Close Activity

In Android, if you want to turn off the current Activity, you can use the finish () method provided by the Activity class.

Example: start and shut down Activity

The core code is as follows

/ / MainActivitypublic class MainActivity extends Activity {@ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); getWindow () .setFlags (WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN, WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN); TextView password = (TextView) findViewById (R.id.wang_mima) Password.setOnClickListener (new View.OnClickListener () {@ Override public void onClick (View v) {/ / create Intent object Intent intent = new Intent (MainActivity.this,PasswordActivity.class); / / launch PasswordActivity startActivity (intent);}});}}

Get the main interface

/ / create a new activity PasswordActivitypackage com.example.example61;import androidx.appcompat.app.AppCompatActivity;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.ImageButton;public class PasswordActivity extends Activity {@ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_password) / / get the close button in the layout file ImageButton close = (ImageButton) findViewById (R.id.close); close.setOnClickListener (new View.OnClickListener () {@ Override / / close the current Activity public void onClick (View v) {finish ();}});}}

Click the password recovery interface

These are all the contents of the article "how to use Activity components in Android". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to 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