android生命周期_Android活动生命周期

android生命周期

In this tutorial you will learn about android activity lifecycle.

在本教程中,您将了解android活动生命周期。

I have already told you in previous tutorial that an activity is a screen or window that interacts with user. It holds all the UI widgets like button, label, textbox, etc.

我已经在上一教程中告诉过您,活动是与用户交互的屏幕或窗口。 它包含所有UI小部件,例如按钮,标签,文本框等。

If you have worked on Java language then you would definitely know that Java program execution starts with main() method. Similarly in Android, Activity class also have a call back method named as onCreate(). When an activity is created, the first method called is onCreate().

如果您使用过Java语言,那么您肯定会知道Java程序的执行从main()方法开始。 同样在Android中, Activity类也有一个名为onCreate()的回调方法。 创建活动后,第一个调用的方法是onCreate()。

There are 6 other call back methods that are called at different stages in activity lifecycle. The java class associated with an activity should extend Activity class. And that java class must override at least onCreate() method.

在活动生命周期的不同阶段还有6种其他的回调方法。 与活动关联的Java类应扩展Activity类。 并且该Java类必须至少重写onCreate()方法。

Below I have shared a flow chart that shows different stages of android activity lifecycle.

下面我共享了一个流程图,该流程图显示了android活动生命周期的不同阶段。

Android Activity Lifecycle

Image Source

图片来源

onCreate() – It is called when the activity is first created.

onCreate() –首次创建活动时调用。

onStart() – It is called when the activity becomes visible to the user.

onStart() –当活动对用户可见时调用。

onResume() – It is called when the user starts interacting with the application.

onResume() –用户开始与应用程序进行交互时调用。

onPause() – This method is called when the current activity is being paused and the previous activity is being resumed.

onPause() –当前活动正在暂停并且上一个活动正在恢复时,将调用此方法。

onStop() – It is called when the activity is no longer visible to the user.

onStop() –当活动不再对用户可见时调用。

onDestroy() – It is called before the activity is destroyed by the system.

onDestroy() –在活动销毁系统之前调用它。

onRestart() – It is called when the activity restarts after stopping it.

onRestart() –在活动停止后重新启动活动时调用。

Android活动生命周期示例 (Android Activity Lifecycle Example)

Below I have shared an example to demonstrate android activity lifecycle. In this example I have used Log.d() method to show log messages. It is a method used for debugging purpose.

下面我分享了一个示例来演示android活动生命周期。 在此示例中,我使用Log.d()方法显示日志消息。 这是用于调试目的的方法。

Just create a new project with package name com.thecrazyprogrammer.helloworld and paste following code in MainActivity.java file and run the app. Try to observe the log messages in logcat window.

只需使用包名称com.thecrazyprogrammer.helloworld创建一个新项目,然后将以下代码粘贴到MainActivity.java文件中,然后运行该应用程序即可。 尝试观察logcat窗口中的日志消息。

package com.thecrazyprogrammer.helloworld;
 
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
 
public class MainActivity extends Activity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d("Log-------","onCreate()");
    }
 
    @Override
    protected void onStart() {
        super.onStart();
        Log.d("Log-------","onStart()");
    }
 
    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d("Log-------","onRestart()");
    }
 
    @Override
    protected void onResume() {
        super.onResume();
        Log.d("Log-------","onResume()");
    }
 
    @Override
    protected void onStop() {
        super.onStop();
        Log.d("Log-------","onStop()");
    }
 
    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d("Log-------","onDestroy()");
    }
 
    @Override
    protected void onPause() {
        super.onPause();
        Log.d("Log-------","onPause()");
    }
}

You can open log messages by following the steps marked in below image. Click on different buttons like menu, call, back, etc on your emulator and observe the different log messages to understand when and which call back method is called.

您可以按照下图标记的步骤打开日志消息。 单击仿真器上的菜单,调用,返回等不同按钮,并观察不同的日志消息以了解何时以及调用哪种回调方法。

Android Activity Lifecycle Example

If you found anything incorrect or have doubts related to above android activity lifecycle tutorial then feel free to comment below.

如果您发现任何错误或对以上android活动生命周期教程有疑问,请在下面发表评论。

翻译自: https://www.thecrazyprogrammer.com/2015/11/android-activity-lifecycle.html

android生命周期

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值