“This is the fifth day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

The four components of Android are Activity, Service, Broadcast Receiver, and Content Provider. Next, detailed acceptance of the four parts will be carried out

Four components of Activity

An Activity is usually presented as a visual interface, and in Android development, an Activity must be exposed to it. It is important to understand its life cycle deeply, how it is created and how it is used.

Activity State

There are four active states:

  • Running Status: This Activity is at the top of the Android Activity stack.
  • Paused: When an Activity loses focus but is still visible (the Activity is partially obscured)
  • Stopped state: completely overwritten by another Activity.
  • Destroyed state: The Activity is destroyed by the system.

Activity Lifecycle

  1. onCreate()

    Called when the Activity is created, at the beginning of the lifecycle. Activity initialization, view creation, view binding.

  2. onStart()

    Actiyity creation or re-entry from the background to the foreground is performed. It and the onCreate() method are primarily used to initialize the object.

  3. onResume()

    Called when the Activity is about to start interacting with the user. After execution, it represents the current activity. In this case, the activity is at the top of the stack of activities

  4. onPause()

    Called when another Activity enters the foreground screen. Some data is persisted during this period and other CPU-intensive operations are stopped. OnPause () is executed whenever something happens that causes the program to suddenly die. OnSaveInstanceState () can hold some temporary data.

  5. onStop()

    If the Activity is not at the top of the UI, it is called when it is completely obscured from view, mainly to release and recycle resources. During this time, operations such as unregistering broadcasts can generally be performed because the user is not visible.

  6. onDestory()

    Called when the Activity is destroyed.

The specific process is shown in the diagram below to show the life cycle of the Activity more clearly and intuitively:

The Activity of the boot

Implicit start

In the androidmanifest.xml file

<activity android:name=".MainActivity">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
Copy the code

You can start an Activity by knowing its action, without knowing the class name or package name.

Intent intent = new Intent();
intent.setAction("android.intent.action.MAIN");
startActivity(intent);
Copy the code

Explicit start

Intent intent = new Intent(MainActivity.this,HomeActivity.class);
startActivity(intent);
​
​
Intent intent = new Intent();
intent.setClassName(getPackageName(),getPackageName()+".HomeActivity");
startActivity(intent);
Copy the code