阅读英文原版和看翻译是不一样的,看英文更容易理解其中的内涵。打算利用空余时间阅读官方的Training,并记录下笔记和自己的一些思考。如有理解有误的地方,欢迎指正。
前两节基本是介绍,不作笔记了。直接从生命周期开始记录。
先看看官方对android生命周期的一个宏观介绍:
Unlike other programming paradigms in which apps are launched with a main()
method, the Android system initiates code in an Activity
instance by invoking specific callback methods that correspond to specific stages of its lifecycle. There is a sequence of callback methods that start up an activity and a sequence of callback methods that tear down an activity.
介绍了android程序与其他程序不一样的地方。
生命周期流程图,google已改为金字塔形式:
对插图的介绍:
Figure 1. A simplified illustration of the Activity lifecycle, expressed as a step pyramid. This shows how, for every callback used to take the activity a step toward the Resumed state at the top, there's a callback method that takes the activity a step down. The activity can also return to the resumed state from the Paused and Stopped state.
1.Does not crash if the user receives a phone call or switches to another app while using your app.
2.Does not consume valuable system resources when the user is not actively using it.
3.Does not lose the user's progress if they leave your app and return to it at a later time.
4.Does not crash or lose the user's progress when the screen rotates between landscape and portrait orientation.
However, only three of these states can be static. That is, the activity can exist in one of only three states for an extended period of time:
Resumed
In this state, the activity is in the foreground and the user can interact with it. (Also sometimes referred to as the "running" state.)
Paused
In this state, the activity is partially obscured by another activity—the other activity that's in the foreground is semi-transparent or doesn't cover the entire screen. The paused activity does not receive user input and cannot execute any code.
Stopped
In this state, the activity is completely hidden and not visible to the user; it is considered to be in the background. While stopped, the activity instance and all its state information such as member variables is retained, but it cannot execute any code.
If either the MAIN action or LAUNCHER category are not declared for one of your activities, then your app icon will not appear in the Home screen's list of apps.
没有声明入口Activity,手机则不会出现app的图标:
..省略..the system creates every new instance of Activity by calling its onCreate() method.
You must implement the onCreate() method to perform basic application startup logic that should happen only once for the entire life of the activity. For example, your implementation of onCreate() should define the user interface and possibly instantiate some class-scope variables.
所有的Activity都是通过onCreate()方法实例化的。
Once the onCreate() finishes execution, the system calls the onStart() and onResume() methods in quick succession. Your activity never resides in the Created or Started states. Technically, the activity becomes visible to the user when onStart() is called, but onResume() quickly follows and the activity remains in the Resumed state until something occurs to change that, such as when a phone call is received, the user navigates to another activity, or the device screen turns off.
应用不会再onCreate()和onStart()中驻留,都是快速执行过去的。从onStart()开始即可见,只不过时间很短暂即进入onResume()阶段。
While the activity's first lifecycle callback is onCreate(), its very last callback is onDestroy(). The system calls this method on your activity as the final signal that your activity instance is being completely removed from the system memory.
Most apps don't need to implement this method because local class references are destroyed with the activity and your activity should perform most cleanup during onPause() and onStop(). However, if your activity includes background threads that you created during onCreate() or other long-running resources that could potentially leak memory if not properly closed, you should kill them during onDestroy().
The system calls onDestroy() after it has already called onPause() and onStop() in all situations except one: when you call finish() from within the onCreate() method. In some cases, such as when your activity operates as a temporary decision maker to launch another activity, you might call finish() from within onCreate() to destroy the activity. In this case, the system immediately calls onDestroy() without calling any of the other lifecycle methods.