📜  Android-活动

📅  最后修改于: 2021-01-05 04:50:53             🧑  作者: Mango


活动代表具有用户界面的单个屏幕,就像Java的窗口或框架一样。Android活动是ContextThemeWrapper类的子类。

如果您使用过C,C++或Java编程语言,那么您一定已经看到您的程序从main()函数。与之非常相似,Android系统使用Activity来启动其程序,该Activity以对onCreate()回调方法的调用开始。有一系列启动活动的回调方法和一系列拆除活动的回调方法,如下面的活动生命周期图所示:(图片由android.com提供)

Android Activity生命周期

Activity类定义以下回调,即事件。您不需要实现所有的回调方法。但是,重要的是,您必须了解每一个,并实施那些确保您的应用程序符合用户期望的行为。

Sr.No Callback & Description
1

onCreate()

This is the first callback and called when the activity is first created.

2

onStart()

This callback is called when the activity becomes visible to the user.

3

onResume()

This is called when the user starts interacting with the application.

4

onPause()

The paused activity does not receive user input and cannot execute any code and called when the current activity is being paused and the previous activity is being resumed.

5

onStop()

This callback is called when the activity is no longer visible.

6

onDestroy()

This callback is called before the activity is destroyed by the system.

7

onRestart()

This callback is called when the activity restarts after stopping it.

本示例将带您通过简单的步骤来展示Android应用程序活动的生命周期。请按照以下步骤修改我们在“ Hello World示例”一章中创建的Android应用程序-

Step Description
1 You will use Android studio to create an Android application and name it as HelloWorld under a package com.example.helloworld as explained in the Hello World Example chapter.
2 Modify main activity file MainActivity.java as explained below. Keep rest of the files unchanged.
3 Run the application to launch Android emulator and verify the result of the changes done in the application.

以下是修改后的主要活动文件src / com.example.helloworld / MainActivity.java的内容。该文件包括每种基本生命周期方法。 Log.d()方法已用于生成日志消息-

package com.example.helloworld;

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

public class MainActivity extends Activity {
   String msg = "Android : ";
   
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      Log.d(msg, "The onCreate() event");
   }

   /** Called when the activity is about to become visible. */
   @Override
   protected void onStart() {
      super.onStart();
      Log.d(msg, "The onStart() event");
   }

   /** Called when the activity has become visible. */
   @Override
   protected void onResume() {
      super.onResume();
      Log.d(msg, "The onResume() event");
   }

   /** Called when another activity is taking focus. */
   @Override
   protected void onPause() {
      super.onPause();
      Log.d(msg, "The onPause() event");
   }

   /** Called when the activity is no longer visible. */
   @Override
   protected void onStop() {
      super.onStop();
      Log.d(msg, "The onStop() event");
   }

   /** Called just before the activity is destroyed. */
   @Override
   public void onDestroy() {
      super.onDestroy();
      Log.d(msg, "The onDestroy() event");
   }
}

活动类使用项目的res / layout文件夹中可用的XML文件加载所有UI组件。以下语句从res / layout / activity_main.xml文件加载UI组件:

setContentView(R.layout.activity_main);

一个应用程序可以具有一个或多个活动,而没有任何限制。您为应用程序定义的每个活动都必须在AndroidManifest.xml文件中声明,并且应用程序的主要活动必须在清单中使用声明,其中包括MAIN操作和LAUNCHER类别,如下所示:




    
        
            
                

                
            
        
    


如果未为您的一项活动声明MAIN动作或LAUNCHER类别,则您的应用程序图标将不会出现在主屏幕的应用程序列表中。

让我们尝试运行修改后的Hello World!我们刚刚修改的应用程序。我假设您在进行环境设置时已创建了AVD 。要从Android Studio运行该应用,请打开您项目的活动文件之一,然后点击运行Eclipse运行图标工具栏中的图标。 Android studio将应用程序安装在您的AVD上并启动它,如果设置和应用程序一切正常,它将显示Emulator窗口,并且您应该在Android studio的LogCat窗口中看到以下日志消息-

08-23 10:32:07.682 4480-4480/com.example.helloworld D/Android :: The onCreate() event
08-23 10:32:07.683 4480-4480/com.example.helloworld D/Android :: The onStart() event
08-23 10:32:07.685 4480-4480/com.example.helloworld D/Android :: The onResume() event

Android LotCat视窗

让我们尝试单击Android模拟器上的锁屏按钮,它将在android studio的LogCat窗口中生成以下事件消息:

08-23 10:32:53.230 4480-4480/com.example.helloworld D/Android :: The onPause() event
08-23 10:32:53.294 4480-4480/com.example.helloworld D/Android :: The onStop() event

让我们再次尝试在Android模拟器上解锁屏幕,它将在Android Studio的LogCat窗口中生成以下事件消息:

08-23 10:34:41.390 4480-4480/com.example.helloworld D/Android :: The onStart() event
08-23 10:34:41.392 4480-4480/com.example.helloworld D/Android :: The onResume() event

接下来,让我们再次尝试单击“后退”按钮Android后退按钮在Android模拟器上运行,它将在Android Studio的LogCat窗口中生成以下事件消息,从而完成了Android应用程序的活动生命周期。

08-23 10:37:24.806 4480-4480/com.example.helloworld D/Android :: The onPause() event
08-23 10:37:25.668 4480-4480/com.example.helloworld D/Android :: The onStop() event
08-23 10:37:25.669 4480-4480/com.example.helloworld D/Android :: The onDestroy() event