Basic introduction and usage of Android Activity (Activity)

The first line of codeAndroid

Chapter two

Start with what you can see—exploration activities

Activity is the easiest place to attract users. It is a component that contains user interface and is mainly used to interact with users.

 

1. The basic usage of the activity

1. Manually create an event

Create an empty activity

2. Create and load the layout

Create a .xml file

 

What is added here is a Button control, id is the unique identifier of this element, width and height are the width and height of this element, and text is the content displayed in this element. match_parent means to make the current element the same as the parent element (that is, the entire phone screen), and wrap_content means to just wrap the content of the element 

Then add to the newly created empty activity in the first step

 Call setContentView() to load this layout for this empty activity, and there is a Button control in the layout

 

3. Register in the AndroidManifes file

Make the created empty activity the main activity of this app

4. Use Tonst in Events

 The Button control is used here, don’t worry, this will be learned in detail in the later UI controls, first obtain the defined elements in the layout file through the findViewById() method, and pass in R.id.button_1 to get the instance of the Button button , register a listener for the button through setOnClickListener(), and the method in onClick() will be executed when the button is clicked.

5. Using Menu in an Activity

Create a menu file in the res file, and then create a Menu resource file file in the menu file, and enter the file name as main.

Here we have created two menu items, and the <item> tag is used to create a specific menu item. Then go back to the empty activity and rewrite the onCreatOptionsMenu() method  here just to display the menu, which is definitely not enough, so we have to define two response events for the menu (that is, what happens when you click on the menu), here we rewrite onOptionsItemSelected ()method

 

 

 

Then click Run, you will find that there are three more dots in the upper right corner of the phone, yes, this is the menu key I just defined.

6. Destroy an activity

Destroying the activity only needs to change the code of the button listener 

 

 

Two, use Intent to shuttle between activities

Intent is an important way to interact between various components in an Android program. It can not only indicate the action that the current component wants to perform, but also transfer data between different components.

1. Use an explicit Intent

Intent has multiple constructor overloads, one of which is Intent(Context packageContext, Class<?>cls). This constructor receives two parameters, the first parameter is to provide the context to start the activity, and the second parameter is the Class which is the target activity you want to start.

Here we first build an Intent to pass in FirstActivity.this as the context, and then pass in SecondActivity.class as the target activity (please refer to the creation of the activity for the creation of the second activity here) 

2. Use Implicit Intent

3. Pass data to the next activity

in the first activity

Here we still use the explicit method to start the second activity, and then pass a string through the putExtra() method. The putExtra() method here passes two parameters, the first parameter is the key, which is used to get the value from the Intent later. The second parameter is the actual data to be passed

 In the second activity

 Then we fetch the passed data in the second activity and print it. We use the getIntent() method to get the

Activate the Intent of the second activity, then call the getStringExtra() method, and pass in the key value of the response to get the data we passed in.

Here we pass a string using getStringExtra(), if we pass an integer data, use getIntExtra(), if we pass a boolean type of data, use getBooleanExtra().

4. Return data to the previous activity

The startActivityFooResult() method can also be used to start an activity, but this method expects to return a result to the previous activity when the activity is destroyed

In the first activity, modify the button click event of the first activity

startActivityFooResult() has two parameters, the first is Intent, and the second is a request code, which is used to determine the source of data in subsequent callbacks.

 In the second activity, we can see that we also define an Intent in the second method, but this Intent does not have any specified intent, it is only used to transfer data. At this point the button in the second activity says, destroy the second activity.

 

 

in the first activity

 Because we use startActivityFooResult() to start the second activity in the first activity, after the second activity is destroyed, onActivityResult() in the first activity will be called, so we are in the first activity Rewrite onActivityResult() in to get the data returned in the second activity.

onActivityResult() has three parameters. The first parameter, requestCode, is the request code we passed in when we started the activity. The second parameter is resultCode, which is the processing result we passed in when we returned the data (just in the first The number 1 passed in the activity, the third parameter is the Intent of the data returned by the carrier.

In the second activity, another situation

If we don't click the button in Activity 2 to destroy Activity 2, but destroy Activity 2 by returning and go back to Activity 1, we can add the onBackPressed() method to solve it.

 3. Activity life cycle

1. Return stack

 

2. Activity status

 

3. The lifetime of the activity

 

4. Experience the life cycle of activities

 4. Activity start mode

1.standard

Every time a new activity is started, it will be pushed into the return stack and placed at the top of the stack

 

2.singleTop

If you find that the top of the return stack is the activity to be started, you can use it directly without creating a new activity instance

 

3.singleTask

Every time an activity is started, the system will first check whether there is an instance of the startup activity in the return stack. If it finds an existing instance, it will directly use the instance and pop all the activities above the activity out of the stack. If not found The created instance will create an instance of

 

4.singleInstance

This mode starts a new return stack to manage this activity shared with other programs

 

Summarize

Congratulations, you have learned most of the knowledge points of the event!

Reference Android first line of code (version 2)

Guess you like

Origin blog.csdn.net/qq_65337539/article/details/125945984