Android学习之Activity生命周期文档翻译

来源:互联网 发布:影像混搭软件 编辑:程序博客网 时间:2024/05/22 12:30

刚下班回来,翻译一篇简单的,加深理解。

文档地址:http://guides.codepath.com/android/Activity-Lifecycle#activity-lifecycle

Activity Lifecycle

As a user navigates throughout an app, Android maintains the visited activities in a stack, with the currently visible activity always placed at the top of the stack.

At any point in time a particular activity can be in one of the following 4 states:

当用户浏览一个app,Android的activitys在堆栈中以堆叠形式保持着,当前可见的activity总是位于栈顶

在任何时间的特定活动总会保持下面4中状态之一:

这里写图片描述

运行状态:活动可见并且可以与用户交互

暂停状态:活动可见,但是不能与用户交互

停止状态:活动不可见

杀死状态:活动已经被系统杀死或者调用了finish()方法

Activity Lifecycle

The following diagram shows the important state paths of an Activity.
The square rectangles represent callback methods you can implement to
perform operations when the Activity moves between states. These are
described further in the table below the diagram.

下面的图展示了活动比较重要的状态路径,你可以去实现长方形里面的回调方法,执行操作时活动在不同状态间的移动,这些在表中图下面做进一步描述

这里写图片描述

生命周期:oncrete,onstart,onpause,onstop,onrestart,ondestory。

运行状态:在onresume—>onpause之间

杀死状态:在onstop—>ondestory之间

暂停状态:在onpause—>onstop之间

进程被杀死:一般是在onpause,onstop—>oncreate之间

onrestart:在onstop—》onstart之间

这里写图片描述

onCreate:活动在创建,但是对于用户不可见,用户大部分初始化代码放在这个方法里,通过setContentView来为活动初始化布局

onstart:活动可见,但是还没准备好与用户交互,这个方法并没有用太多,可以注册一个广播来监听影响用户UI的改变,因为这个方法UI已经可见。

没搞过,不太懂?

onresume:活动可见,可以与用户交互,这是一个好的方法来启动动画,打开类似相机的设备

onpause:这个活动已经进入后台,并且停止与用户交互,发生情况是在当前界面启动另外一个活动,通常在这个方法里做保存全局状态,例如写入一个文件

onstop:活动不可见,通常跟onstart中的操作相反。

ondestory:当调用finish(),或者系统需要充足内存就会触发这个方法,通常在这个方法里做各种清除操作,例如活动有一个线程正在后台运行来请求网络数据,它会在oncreate()中创建线程,在ondestory中停止线程

onrestart:再一次启动之前,活动已经被停止。这个方法通常并不需要去实现回调。

1 0
原创粉丝点击