android learning - the first activity learning

install android studio

Install the software: link - android studio installation

Create the first project, download the SDK, install the emulator
Problems encountered when creating:
Could not find any version that matches
Solution: Find the file build.gradle and modify the last dependencies configuration

project framework

The project framework is divided into three parts:

  • The overall framework manifests for the project
  • Logic execution framework java program
  • Interface/picture... etc.

Log debug print

Log.v(LogDemo.ACTIVITY_TAG, "This is Verbose.");//Any message will be output
Log.d(LogDemo.ACTIVITY_TAG, "This is Debug.");//debug print
Log.i(LogDemo.ACTIVITY_TAG, "This is Information");//general informative messages
Log.w(LogDemo.ACTIVITY_TAG, "This is Warnning.");//warning warning
Log.e(LogDemo.ACTIVITY_TAG, "This is Error.");	//error error

activity learning

Source path: Source code github path

Base code:

public class MainActivity extends AppCompatActivity {

    private Button mBtnTextView;
    protected void onCreate(Bundle savedInstanceState) {
    	//Execute the activity's onCreate operation
        Log.d("torres001","hello world");
        //Display the xml layout interface

activity jump implementation

  1. Click on the java directory, right click and select create a new activity. After completion, a corresponding xml layout file will be generated under res
  2. Add a button to the main interface of activity_main.xml for jump control. Click the magic wand, the component will be displayed at the specified position, otherwise the component will be displayed at (0, 0) coordinates by default.
        android:text="second activity"
        app:layout_constraintEnd_toEndOf="parent" />
  1. Modify the program to realize the jump function
	//Define a Botton component
	private Button mBtnTextView;
    protected void onCreate(Bundle savedInstanceState) {
        Log.d("torres001","hello world");
		//Find the created Button component
        mBtnTextView = findViewById(;
        //Implement a button listener event, which is executed when the Button is pressed
        mBtnTextView.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
            	//Create an intent that points to the second activity
                Intent intent = new Intent(MainActivity.this, Main2Activity.class);
                //Start activity jump

Activity role
One of the four major components of Android, an Activity provides a screen through which the user can interact to perform certain actions, such as making a call, taking a photo, sending an email, or viewing a map. Each activity has a window that draws its user interface. The window usually fills the screen, but may be smaller than the screen and float on top of other windows.
An Android application usually consists of multiple Activities that are loosely bound to each other. Typically, an Activity in an application is designated as the "main" Activity, usually the one generated by default when the project is created. This Activity is presented to the user when the application is first launched. Each Activity can then launch another Activity to perform a different action.
Every time a new Activity starts, the previous Activity stops, but the system keeps the Activity on the background stack ("back stack"). When a new Activity starts, it gets pushed onto the back stack and gets the user's attention. The back stack follows the basic "last in first out" stack mechanism, so when the user finishes the current activity and presses the "back button", it is popped off the stack (and destroyed) and the previous activity resumes.

How to start Activity
In addition to the first "main" Activity, other activities need to be started.

  • Start the specified self-built Activity
Intent intent = new Intent(this, SignInActivity.class);
  • Start other types of Activity
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_EMAIL, recipientArray);
  • On startup, send data
    Sometimes, we may need to receive the return data result from the previous Activity. In this case, start the Activity by calling startActivityForResult() (instead of startActivity()). Then, to receive results from subsequent activities, you need to implement the on Activity Result() callback method. After completing the subsequent Activity, it returns the result in the onActivityResult() method.

Activity life cycle detailed explanation
Activity basically exists in three states:
Resume onResume()
The Activity is in the foreground of the screen and has user focus.
Paused onPause()
Another Activity is in the foreground and has focus, but this one is still visible. That is, another Activity is visible on top of this Activity that is partially transparent or does not cover the entire screen. The suspended activity is fully active (the Activity object remains in memory, it retains all state and member information, and remains attached to the window manager),
But can be killed by the system in extremely low memory situations.
stop onStop()
The Activity is completely obscured by another Activity (the Activity is now in the "background"). The stopped activity also still exists (the Activity object remains in memory, it maintains all state and member information, but is not attached to the window manager). However, it is no longer visible to the user, and it can be killed by the system when memory is needed elsewhere. If an Activity is paused or stopped, the system can remove it from memory by asking it to finish (calling its finish() method) or by simply killing its process. When the activity is opened again (after completion or kill), it must be recreated.

  1. onCreate() is called when the Activity is first created
  2. onRestart() is called after the Activity is stopped and before it starts again
  3. onStart() is called before the activity becomes visible to the user
  4. onResume() is called before the Activity starts interacting with the user. At this point, the Activity is at the top of the stack
  5. onStop() is called when the system is about to start resuming another Activity.
  6. onDestroy() is called before the activity is destroyed.

Posted by vandutch on Mon, 23 May 2022 00:08:28 +0300