Latest web development tutorials

Android activity (Activity)

Activity represents a single screen with a user interface, such as Java or window frame. Android activity is a subclass ContextThemeWrapper class.

If you have to use C, C ++ or Java programming language, you should know that these programs from the main () function begins. Very similar, Android system initializes its program is () callback is invoked by the start of the activities onCreate. There is a callback method to initiate a sequence of events, but there are ways to turn off a sequence of events, as in the following activities in the statement cycle shown below:

image

Activity class defines the following callback. You can not implement all of the callback method. When a very important to understand each of them, these can ensure that your application behaves as users expect.

Callback description
onCreate () This is the first callback, the active call is first created
onStart () This callback is invoked when the activity is visible to the user
onResume () This callback is called when the user starts the application and interactive
onPause () Suspended activities can not accept user input, you can not execute any code. When the current activities will be suspended, at an event to be restored by calling
onStop () Called when the activity is not visible
onDestroy () When a system call is destroyed before the event
onRestart () Called when the activity is stopped after reopen

Examples

This example shows Anroid application lifecycle activities by simply steps. Follow these steps to modify the Android application we created in the Hello World Examples section.

step description
1 Use eclipse IDE to create an Android application and name it HelloWorld placed under com.example.helloworld package. Hello World Example section as previously described.
2 Modify the following main activities file MainActivity.java. Holding other parts unchanged.
3 Run the application to open the Android emulator, modify and check the results of the application.

Here are the main activities of the file src / com.example.helloworld / MainActivity.java modified content. Each of which contains a basic life cycle approach. Log.d () method is used to generate the 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");
   }
}

Activity class load all UI components from the project's res / layout of the XML file. The following statement is loaded UI component from the res / layout / activity_main.xml file:

setContentView(R.layout.activity_main);

An application can have one or more activities, without any restriction. Each activity defined for applications that need to be declared in AndroidManifest.xml. The main activities of the application need to be declared in the manifest, and the intent filter labels need to contain MAIN action and LAUNCHER category. 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 MAIN LAUNCHER category action or not declared at the event, then the application's icon will not appear in the application list on the main screen.

Let's run from just modified "Hellow World!" Application. Suppose you've created AVD when setting up the environment. Run the application from Eclipse, open a project in the active file, and click Run from the toolbar image icon. Eclipse install applications on AVD and starts it. If all goes well, the simulator will display the following screen, and you can see the log information in the Eclipse IDE LogCat window:

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 on the red button on the Android Emulator image , It generates the following event message in the Eclipse IDE LogCat window:

<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 on the menu button on the Android emulator again image , It generates the following event message in the Eclipse IDE 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 button on the Android Emulator image , It generates the following event message in the Eclipse IDE LogCat window, the Android application activity throughout the life cycle is completed.

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