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



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值