Android Study Notes 2.1 Activity Overview


1. Learning objectives

  1. Create Activity using two methods
  2. Register Activity in project manifest file
  3. 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
    Insert image description here
  • Click [Document]
    Insert image description here
  • Open the developer guide
    Insert image description here
  • Find related content about Activity
    Insert image description here

(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
    Insert image description here

1. Create a custom Activity based on common classes

  • net.xqf.demo2_1Create a new ordinary Java class in the package
    Insert image description here

  • Enter the class name LoginActivity
    Insert image description here

  • Add documentation comments to classes
    Insert image description here

  • res/layoutCreate layout files in the directory to activity_loginserve LoginActivityas the user interface
    Insert image description here

  • Set layout file name -activity_login
    Insert image description here

  • Click the [Finish] button
    Insert image description here

  • switch to Codeview
    Insert image description here

  • Set linear layout gravity and orientation attributes, add a label, and set related attributes
    Insert image description here

  • Modify LoginActivity and inherit AppCompatActivity
    Insert image description here

  • Create a callback method onCreate()and use layout resource files to set up the user interface
    Insert image description here

  • Now when we start the application, we will see the MainActivity interface. Later we will set LoginActivity to 启动Activityrestart the application and we will see the LoginActivity interface.

2. Create custom activities based on templates

  • Empty ActivityCreate custom activities based on templates
    Insert image description here

  • Configure Activity information
    Insert image description here

  • Click the [Finish] button
    Insert image description here

  • Change to linear layout, set gravity and orientation properties, add a label control, and set related properties
    Insert image description here

  • 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.xmlthe file
    Insert image description here

  • 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
Insert image description here

  • labelYou can set the properties of LoginActivity and RegisterActivyt
    Insert image description here

  • strings.xmlDefine variables in string resource files loginand registermodify app_namevariable values
    Insert image description here

  • Check the Android project manifest file again
    Insert image description here

  • Launch the app and see the effect
    Insert image description here

  • 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
    Insert image description here

2. Set LoginActivity to start Activity

  • Set intent filters for LoginActivity
    Insert image description here

  • Launch the app and see the effect
    Insert image description here

3. Set RegisterActivity to start Activity

  • Set intent filters for RegisterActivyt
    Insert image description here

  • Launch the app and see the effect
    Insert image description here

3. Computer operation

(1) Create button

  • Open activity_register.xmland activity_login.xmlcreate buttons
    Insert image description here
    Insert image description here

(2) Add events to buttons

  • Open RegisterActivityand LoginActivityadd button click event
    Insert image description here
    Insert image description here

(3) Run the program to view the results

Please add image description

Guess you like

Origin blog.csdn.net/qq_41301333/article/details/127796058