Four components of Android (1)

"This is the 5th day of my participation in the November Update Challenge. For details of the event, please check: 2021 Last Update Challenge "

The four major components of Android are Activity, Service, Broadcast Receiver, and Content Provider. Next, we will accept the four parts in detail.

Activity of the four components

An Activity is usually represented as a visual interface. In Android development, Activity must be touched. It is very important to have a deep understanding of its life cycle. It is necessary to master how it is created and used.

Activity activity state

There are four active states:

  • Running state: The Activity is at the top of the Android activity stack.
  • Paused state: refers to the Activity loses focus but is still visible (the Activity is partially obscured)
  • Stopped state: completely covered by another Activity.
  • Destroyed state: means that the Activity is destroyed by the system.

Activity life cycle

  1. onCreate()

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

  2. onStart()

    Actiyity creation or returning from the background to the foreground is performed. It and the onCreate() method are mainly used for the work of initializing objects.

  3. onResume ()

    Called when the Activity will start interacting with the user. After execution, it means the current activity. This is running, and the activity is at the top of the active stack.

  4. onPause ()

    It will be called when another Activity enters the foreground interface. In the meantime, persist some data and stop other CPU-intensive operations. No matter what happens, causing the program to die suddenly, onPause() will be executed. onSaveInstanceState() can save some temporary data.

  5. onStop()

    If the Activity is not at the top of the UI, it will be called when it is completely blocked and invisible, mainly for the release and recycling of resources. During this period, operations such as unregistering the broadcast can generally be performed, because the user is not visible.

  6. onDestory()

    Called when the Activity is destroyed.

For the specific process, please refer to the following schematic diagram to show the life cycle of Activity more clearly and intuitively:

unnamed file(1).png

Activity start

implicit start

Configure 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>
复制代码

As long as you know the action of the activated Activity, you can start the Activity without knowing the specific class name and package name.

Intent intent = new Intent();
intent.setAction("android.intent.action.MAIN");
startActivity(intent);
复制代码

explicit start

Intent intent = new Intent(MainActivity.this,HomeActivity.class);
startActivity(intent);
​
​
Intent intent = new Intent();
intent.setClassName(getPackageName(),getPackageName()+".HomeActivity");
startActivity(intent);
复制代码

Guess you like

Origin juejin.im/post/7027478972065644558