本人菜鸟一枚,因为工作设计Android,不得不学习下这个新东东。在此记录下个人的学习历程,

也希望与各位大神交流学习。不知道该从那本书看起,下载了一些电子版的书籍,但还是忍不住冲动,

先从官方文档开始。

本人计划是: 先大体看一遍官方指南,对很多概念有个初步理解,之后再边用边学。 废话少说,

开启我学习笔记的第一篇 —— Android Activity 。


Understand the Lifecycle Callbacks






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.)
就是位于当前状态下的活动处于前台,用户可以与其UI交互。
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.

The other states (Created and Started) are transient and the system quickly moves from them

to the next state by calling the next lifecycle callback method. That is, after the system calls

onCreate(), it quickly callsonStart(), which is quickly followed byonResume().

上面的Created和Started状态是过渡状态,系统会通过下一个生命周期回调方法快速地从他们

转向下一个状态。也就是说,在系统调用了onCreate之后,他很快会调用onStart,接着onResume。

比如,进入相机,选择非默认模式,如视频拍摄模式,然后按菜单键回到主页,那个视频拍摄对应的Activity

就处于stopped状态。如何确认? 打开最近运行程序的窗口(手机上有此按键),选择相机app,

或者直接再次选择相机app,你将看到,相机进入了上次的那个视频拍摄模式,而非默认的拍照模式,

这是因为: stopped -> onRestart -> onStart -> onResume

当然,如果你结束此相机应用(在最近运行程序的窗口移除,或者进行程序清理这样的动作,或者

按 “返回”按键),然后再进入相机时,将发现相机处于拍照模式。

这是因为: 清除程序后,会调用onDestroy方法,相关的Instance State被清除了,之后再次进入

的时候,是: onCreate -> onStart -> onResume



Specify Your App's Launcher Activity


When the user selects your app icon from the Home screen, the system calls theonCreate()

method for theActivityin your app that you've declared to be the "launcher" (or "main") activity.

This is the activity that serves as the main entry point to your app's user interface.

You can define which activity to use as the main activity in the Android manifest file,

AndroidManifest.xml, which is at the root of your project directory.

The main activity for your app must be declared in the manifest with an<intent-filter>

that includes theMAINaction andLAUNCHERcategory. For example:

<activity android:name=".MainActivity" android:label="@string/app_name">   <intent-filter>     <action android:name="android.intent.action.MAIN" />     <category android:name="android.intent.category.LAUNCHER" />   </intent-filter> </activity>

If either theMAINaction orLAUNCHERcategory are not declared for one of your activities, then your

app icon will not appear in the Home screen's list of apps.

启动活动(主活动)将会作为app UI的主要入口。我们在项目根目录下的AndroidManifest.xml里面指定。

应用程序的主活动,通过在Activity下设定包含MAIN action和LAUNCHER category的intent-filter来指定。

如果上面的二者之一没声明,应用程序图标将不会在Home Screen的应用列表中出现。

从上面的信息得知,一个app是可以有多个Activity的。


Create a New Instance


同一个app中可能有多个Activity,那么当这些activity之间进行切换的时候,Android是如何

管理的呢? 至少得有个类似ID的东东识别不同的Activity吧? 猜的不错,Android里面确实

提供了这样的东东,这也就是为啥我们看onCreate里面总能发现它会去调用onCreate。

the system creates every new instance ofActivityby calling itsonCreate()method.

public void onCreate(Bundle savedInstanceState) {   super.onCreate(savedInstanceState);

至于这个Bundle类型的InstanceState,保存了用来恢复activity相关的状态。

通过覆盖onSavedInstanceState,我们可以添加我们需要保存的其他状态信息(键值对)

到Bundle对象里。

Destroy the Activity


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 duringonPause()andonStop(). However,

if your activity includes background threads that you created duringonCreate()or other long-running

resources that could potentially leak memory if not properly closed, you should kill them duringonDestroy().


大多数情况下,我们不需要实现onDestroy,关于清除的动作,都是在onPause和onStop中完成。


什么时候会调用onDestroy方法呢? 就是当此activity要被完全关闭,并释放资源的时候。除了上面

所说的主动方式(诸如按“返回”按键),当此activity长时间不用,或者前台activity需要更多内存,

而当前内存不够。


Your activity will be destroyed and recreated each time the user rotates the screen. When the

screen changes orientation, the system destroys and recreates the foreground activity because

the screen configuration has changed and your activity might need to load alternative resources

(such as the layout).


当翻转屏幕的时候,系统会销毁和重建前台的activity。 因为screen的设置变了,activity可能需要

加载另外一套资源(如布局)。




参考:http://developer.android.com/intl/zh-cn/training/basics/activity-lifecycle/index.html

(里面对activity讲解的4节)





更多相关文章

  1. Android(安卓)开发技术周报 Issue#287
  2. Android(安卓)- Activity 篇一
  3. Android之Camera的简单实例
  4. 对在Android的状态栏添加Menu,Back,Home的改进
  5. android activity 生命周期详解
  6. android电池充电以及电量检测驱动分析
  7. Android实践--监测网络状态
  8. android activity与fragment的生命周期详细研究
  9. android webview里设置cookie

随机推荐

  1. Android(安卓)打包及引用 aar 文件的方法
  2. Android中的Service的通信
  3. Android应用架构
  4. Android绘图系列(三)——自定义View绘制
  5. Android(安卓)Stuido 导出 APK
  6. 《Android(安卓)JNI》02 对java传进来的
  7. 安卓root漏洞分析——CVE-2009-1185
  8. Android开发管理手机短信
  9. android动画的透明度渐变、旋转动画、缩
  10. Android异步任务AsyncTask的使用与原理分