Android官方Training閱讀筆記 ---- Managing the Activity Lifecycle(Starting an Activity) (一)

閱讀英文原版和看翻譯是不一樣的,看英文更容易理解其中的內涵。打算利用空餘時間閱讀官方的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.

可以看出,谷歌把android生命週期抽象成了金字塔形式,最上面是Resumed可見狀態,即前臺與用戶交互的狀態。


Depending on the complexity of your activity, you probably don't need to implement all the lifecycle methods. However, it's important that you understand each one and implement those that ensure your app behaves the way users expect. Implementing your activity lifecycle methods properly ensures your app behaves well in several ways, including that it:
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.


說的是activity只有三種狀態時可以長時間停留的(相對其他狀態),其他狀態很短時間即執行完畢,然後會立馬調用下一階段的方法:

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.

這裏對Paused進行說明,這裏指的是Activity不被另一個Activity完全覆蓋,即在前臺的Activity是半透明或者沒有充滿屏幕,所以如果彈出一個Dialog,是不會執行此階段的。


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().

程序結束並調用onDestroy()的最終信號是activity實例從內存移除。還介紹了在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.

正常情況都是調用onPause()和onStop()以後再調用onDestroy(),但有一個特例,如果在onCreate()方法裏面finish()了,則直接調用onDestroy()。



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章