Android study notes 05: Activity overview

Zero, learning goals

  1. Can tell the role of Activity
  2. Will create Activity based on template
  3. Will register Activity in the project manifest file
  4. Will be set to start Activity in the project manifest file

Insert picture description here

One, the role of Activity

  • Activity is one of the four core components of Android. Its main function is to provide an interface and interact with users.
  • A program is generally composed of multiple activities, the relationship between each activity is very loose, there is no direct relationship between them. There must be an activity designated as the main activity, which is the first interface displayed when the program starts. Each activity can start other activities at will. Whenever an activity is started, the previous activity is stopped. All started activities in a program are placed in a stack, so the stopped activity is not destroyed, but stored in the stack. The newly started activity is first stored in the stack and then gets the input focus. Click the return button on the currently active activity, it is removed from the stack, and then destroyed, and then the previous activity is restored.
  • When an activity is stopped because a new activity is started, it will be notified of a state change. There are multiple such changes, each of which will cause the system to call a corresponding callback method to notify the activity. These callback methods are collectively referred to as "Life cycle callback method". These callback methods are called when the Activity is created, stopped, resumed, and destroyed.

Two, create a custom Activity

  • Create Android application [ActivityOverview]
    Insert picture description here

1. Create based on common class inheriting Activity

  • New Java class
    Insert picture description here
  • Enter the class name
    Insert picture description here
  • Add documentation comments to the class
    Insert picture description here
  • Create a layout file activity_login for LoginActivity as the user interface
    Insert picture description here
  • Set the layout file name-activity_login
    Insert picture description here
    Insert picture description here
  • Switch to Code view
    Insert picture description here
  • Set linear layout attributes, add a label, set attributes
    Insert picture description here
  • Modify LoginActivity, inherit AppCompatActivity
    Insert picture description here
  • Create a callback method onCreate(), use the layout resource file to set the user interface
    Insert picture description here

2. Created based on the Activity template of Android Studio

  • Create a custom activity based on the Empty Activity template
    Insert picture description here
  • Configure Activity information
    Insert picture description here
    Insert picture description here
  • Add a label control to the layout file
    Insert picture description here

Three, register a custom Activity

1. View the project list file

Insert picture description here

  • Only the custom Activity created by inheriting the Activity itself needs to be registered in the project manifest file. The LoginActivity we created just now needs to be registered in the project manifest file by ourselves, and the RegisterActivity created based on the template has been registered for us by the system.

2. Register LoginActivity

Insert picture description here

  • You can set the properties of LoginActivity
    Insert picture description here
  • Define the login variable in the string resource file strings.xml
    Insert picture description here
  • Check the project list file again
    Insert picture description here
  • Start the application and check the effect
    Insert picture description here
  • What you see is the MainActivity window. If you want to see the LoginActivity window first when you want to start the application, you must set LoginActivity to start the Activity (Launcher Activity) in the project manifest file.

Fourth, set to start Activity

  • Set to start Activity by adding intent filter
    Insert picture description here

1. Set LoginActivity to start Activity

  • Set up intent filter
    Insert picture description here
  • Start the application and check the effect
    Insert picture description here

2. Set RegisterActivity to start Activity

  • Set up intent filter
    Insert picture description here
  • Start the application and check the effect
    Insert picture description here
  • Questions: If the MainActivity is started, how can I jump to LoginActivity or RegisterActivity?

Guess you like

Origin blog.csdn.net/howard2005/article/details/108558183