Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Android Activity


May 21, 2021 Android


Table of contents


Android Activity

An activity represents a single screen with a user interface, such as a window or frame in Java. Android's activity is a sub-class of the ContextThemeWrapper class.

If you've ever programmed in the C, C, or Java languages, you should know that these programs start with the main() function. S imilarly, the Android system initializes its program by calling the onCreate() callback in the activity. There is a sequence of callback methods to start an activity, while there is a sequence of methods to close an activity, as shown in the following activity declaration cycle diagram:

Android Activity

The Activity class defines the following callbacks. Y ou don't have to implement all the callback methods. When it's important to understand each of these, implementing these ensures that your app behaves as users expect.

Callback Describe
onCreate() This is the first callback, and the first creation in the activity is called
onStart() This callback is called when the activity is visible to the user
onResume() This callback is called when the application starts interacting with the user
onPause() Suspended activities cannot accept user input and cannot execute any code. When the current activity is about to be paused, the last activity that is about to be resumed is called
onStop() Called when the activity is not visible
onDestroy() Called before the activity is destroyed by the system
onRestart() Called when the activity is stopped and then reopened

This example shows the lifecycle of Anroid application activity in simple steps. Follow these steps to modify the Android application we created in the Hello World Instances section.

Steps Describe
1 Use eclipse IDE to create an Android app named HelloWorldcom.example.helloworld. As described in the Hello World Example section earlier.
2 Follow the changes to the main activity file MainActivity .java. Keep the rest unchanged.
3 Run the app to open the Android simulator and check the results of the app's modifications.

Here's what the main activity file src/com.example.helloworld/mainActivity .java be modified. I t contains each basic lifecycle approach. The Log.d() method is used to generate log information:

package com.example.helloworld;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;

public class MainActivity extends Activity {
   String msg = "Android : ";

   /** 当活动第一次被创建时调用 */
   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      Log.d(msg, "The onCreate() event");
   }

   /** 当活动即将可见时调用 */
   @Override
   protected void onStart() {
      super.onStart();
      Log.d(msg, "The onStart() event");
   }

   /** 当活动可见时调用 */
   @Override
   protected void onResume() {
      super.onResume();
      Log.d(msg, "The onResume() event");
   }

   /** 当其他活动获得焦点时调用 */
   @Override
   protected void onPause() {
      super.onPause();
      Log.d(msg, "The onPause() event");
   }

   /** 当活动不再可见时调用 */
   @Override
   protected void onStop() {
      super.onStop();
      Log.d(msg, "The onStop() event");
   }

   /** 当活动将被销毁时调用 */
   @Override
   public void onDestroy() {
      super.onDestroy();
      Log.d(msg, "The onDestroy() event");
   }
}

The active class loads all UI components from the XML file in the project's res/layout. The following statement loads UI components from activity_main.xml/layout/file:

setContentView(R.layout.activity_main);

An application can have 1 or more activities without any restrictions. E ach activity defined for the application needs to be declared .xml Android Manifest. T he main activities of the app need to be declared in the manifest, and the intent filter label needs to contain the MAIN action and LAUNCHER categories. As follows:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.helloworld"
   android:versionCode="1"
   android:versionName="1.0" >

   <uses-sdk
      android:minSdkVersion="8"
      android:targetSdkVersion="22" />

   <application
       android:icon="@drawable/ic_launcher"
       android:label="@string/app_name"
       android:theme="@style/AppTheme" >

       <activity
          android:name=".MainActivity"
          android:label="@string/title_activity_main" >

          <intent-filter>
             <action android:name="android.intent.action.MAIN" />
             <category android:name="android.intent.category.LAUNCHER"/>
          </intent-filter>

       </activity>

   </application>
</manifest>

Whether the MAIN action or the LAUNCHER category are not declared in the activity, the app icon will not appear in the list of apps on the home screen.

Let's run the "Hellow World!" application that we just modified. L et's say you created the AVD when the environment was built. R un the app from Eclipse, open an active Android Activity file in a project, and tap the Run icon from the toolbar. E clipse installs the app on the AVD and launches it. If all goes well, the emulator screen will be displayed as follows, and you will see the log information in the LogCat window of the Eclipse IDE:

07-19 15:00:43.405: D/Android :(866): The onCreate() event
07-19 15:00:43.405: D/Android :(866): The onStart() event
07-19 15:00:43.415: D/Android :(866): The onResume() event

Let's click the Android Activity red button on the Android emulator, which will generate the following event message in the LogCat window of the Eclipse IDE:

<code>07-19 15:01:10.995: D/Android :(866): The onPause() event
07-19 15:01:12.705: D/Android :(866): The onStop() event
</code>

Let's click the menu Android Activity button on the Android Emulator again, which will generate the following event message in eclipse IDE's LogCat window:

<code>07-19 15:01:13.995: D/Android :(866): The onStart() event
07-19 15:01:14.705: D/Android :(866): The onResume() event
</code>

Next, let's click the back Android Activity button on the Android emulator, which will generate the following event message in the Eclipse IDE's LogCat window, and the entire life cycle of the activity on the Android app is complete.

07-19 15:33:15.687: D/Android :(992): The onPause() event
07-19 15:33:15.525: D/Android :(992): The onStop() event
07-19 15:33:15.525: D/Android :(992): The onDestroy() event