【Android】Activity生命周期

先上图啊,清晰明了:

抓住三个状态就可以搞清楚具体的生命周期切换的函数调用了:

The entire lifetime of an activity happens between the first call to onCreate(Bundle) through to a single final call to onDestroy(). An activity will do all setup of "global" state in onCreate(), and release all remaining resources in onDestroy(). For example, if it has a thread running in the background to download data from the network, it may create that thread in onCreate() and then stop the thread in onDestroy().

完整的生命期:调用onCreate方法——调用onDestroy方法期间,Activity会在onCreate里面初始化变量,然后在onDestroy里面释放所有的资源。举个例子,如果一个线程在后台从网络上下载数据,那么它可能会在onCreate里面创建线程,然后在onDestroy里面停止线程。

The visible lifetime of an activity happens between a call to onStart() until a corresponding call to onStop(). During this time the user can see the activity on-screen, though it may not be in the foreground and interacting with the user. Between these two methods you can maintain resources that are needed to show the activity to the user. For example, you can register a BroadcastReceiver in onStart() to monitor for changes that impact your UI, and unregister it in onStop() when the user no longer sees what you are displaying. The onStart() and onStop() methods can be called multiple times, as the activity becomes visible and hidden to the user.

可见生命期:调用onStart方法——调用onStop方法期间,在这段时间以内,Activity在屏幕上是可见的,尽管它可能不是在最前面,不能和用户交互。在这两个方法之间你可以维护向用户显示activity所需要的资源。举个例子,你可以在onStart里面注册一个广播来监听会影响UI的变化,然后当用户完全不能看见界面的时候(此时会调用onDestroy)在onStop方法里面去掉注册,两个方法可以被多次调用。

The foreground lifetime of an activity happens between a call to onResume() until a corresponding call to onPause(). During this time the activity is in front of all other activities and interacting with the user. An activity can frequently go between the resumed and paused states -- for example when the device goes to sleep, when an activity result is delivered, when a new intent is delivered -- so the code in these methods should be fairly lightweight.

前台生命期:调用onResume方法——调用onPause方法期间,在这个期间,activity位于所有的activity之前,并且可以与用户发生交互,一个activity可以经常在resumes和paused方法之间切换——举例子,当设备睡眠的时候,当一个新的intent触发的时候——所以在这两个方法的代码应该是轻量级的。

原文地址:https://www.cnblogs.com/lqminn/p/2728692.html