Article directory
1. Learning objectives
- Create Activity using two methods
- Register Activity in project manifest file
- Set an Activity to start the Activity
2. Learning new lessons
- To learn Android development, the important information is the Android developer resources provided by Google:
https://developer.android.google.cn
- Click [Document]
- Open the developer guide
- Find related content about Activity
(1) Introduction to Activity
- The Activity class is a key component of Android applications, and the way an Activity is launched and composed is a fundamental part of the platform's application model. Unlike the programming paradigm where an application is launched through the main() method, the Android system starts code in an Activity instance by calling specific callback methods corresponding to specific stages of its life cycle.
- The mobile app experience differs from the desktop experience in that the user's interaction with the app doesn't always start in the same place, but often in an undefined way. For example, if you open the email app from the home screen, you might see a list of emails, or if you launch the email app from a social media app, you might be taken directly to the email app's message composition interface.
- The purpose of the Activity class is to facilitate this paradigm. When an application calls another application, the calling application calls the Activity in the other application, not the entire application. In this way, the Activity acts as the entry point for the application to interact with the user. You can implement Activity as a subclass of Activity class.
- Activity provides a window in which an application can draw its interface. This window usually fills the screen, but may be smaller than the screen and float on top of other windows. Typically, an Activity implements a screen in an application. For example, one activity in your app implements the "Preferences" screen, and another activity implements the "Select Photos" screen.
- Most apps contain multiple screens, which means they contain multiple Activities. Typically, an activity in an application is designated as the main activity, which is the first screen that appears when the user launches the application. Each Activity can then start another Activity to perform different operations. For example, the main Activity in a simple email application might provide a screen that displays your email inbox. The main activity may launch other activities from this screen to provide a screen for performing tasks such as writing and opening mail.
- Although various activities in the application work together to form a unified user experience, each activity is only loosely related to other activities, and the dependencies between different activities in the application are usually very small. In fact, Activities often start Activities belonging to other applications. For example, a browser app might launch a social media app's "Share" activity.
- To use an Activity in your app, you must register information about the Activity in the app's manifest, and you must manage the Activity's lifecycle appropriately.
(2) Create a custom Activity
- Create Android application Demo2_1 based on Empty Activity
1. Create a custom Activity based on common classes
-
net.xqf.demo2_1
Create a new ordinary Java class in the package
-
Enter the class name LoginActivity
-
Add documentation comments to classes
-
res/layout
Create layout files in the directory toactivity_login
serveLoginActivity
as the user interface
-
Set layout file name -
activity_login
-
Click the [Finish] button
-
switch to
Code
view
-
Set linear layout gravity and orientation attributes, add a label, and set related attributes
-
Modify LoginActivity and inherit AppCompatActivity
-
Create a callback method
onCreate()
and use layout resource files to set up the user interface
-
Now when we start the application, we will see the MainActivity interface. Later we will set LoginActivity to
启动Activity
restart the application and we will see the LoginActivity interface.
2. Create custom activities based on templates
-
Empty Activity
Create custom activities based on templates
-
Configure Activity information
-
Click the [Finish] button
-
Change to linear layout, set gravity and orientation properties, add a label control, and set related properties
-
In actual application development, we recommend the second method to create a custom Activity.
(3) Register a custom Activity
1. View the project list file
-
Click on
AndroidManifest.xml
the file
-
Only custom activities created by inheriting AppCompatActivity need to be registered in the project manifest file. The LoginActivity we just created requires us to register it in the project manifest file ourselves, and the RegisterActivity created based on the template has been registered for us by the system.
2. Register LoginActivity
<application>
-Add an element to the element and <activity>
register LoginActivity
-
label
You can set the properties of LoginActivity and RegisterActivyt
-
strings.xml
Define variables in string resource fileslogin
andregister
modifyapp_name
variable values
-
Check the Android project manifest file again
-
Launch the app and see the effect
-
What you see is the MainActivity window. If you want to see the LoginActivity window first when you start the application, then you must set LoginActivity to the Launcher Activity (Launcher Activity) in the project manifest file.
(4) Set up and start Activity
1. Set the method to start Activity
- Add it to
<activity>
the element意图过滤器
to set the startup Activity
2. Set LoginActivity to start Activity
-
Set intent filters for LoginActivity
-
Launch the app and see the effect
3. Set RegisterActivity to start Activity
-
Set intent filters for RegisterActivyt
-
Launch the app and see the effect
3. Computer operation
(1) Create button
- Open
activity_register.xml
andactivity_login.xml
create buttons
(2) Add events to buttons
- Open
RegisterActivity
andLoginActivity
add button click event