Activity在Android是一个极其常用类,与用户交互离不开它。

我们先看一段Google对Activity的注释,开头有一段是这样的:An activity is a single, focused thing that the user can do.  Almost all activities interact with the user, so the Activity class takes care of creating a window for you in which you can place your UI with{@link #setContentView}.  While activities are often presented to the user as full-screen windows, they can also be used in other ways: as floating windows (via a theme with{@link android.R.attr#windowIsFloating} set) or embedded inside of another activity (using{@link ActivityGroup}).

上面一段主要就是说,Activity主要是用来创建一个与用户交互的窗口,通过setContentView方法,可以将各种UI放在这个窗口上。Activity可以是全屏的窗口或者说是介面,也是可以悬浮的,也可以一个Activity嵌套包含在另一个Activity里形成一个Activity组合(ActivityGroup)。这里要说一下是,ActivityGroup,Google已建议在HONEYCOMB以后的开发中,用Fragment和FragmentManager来代替。
接下来,看下面一段注解:There are two methods almost all subclasses of Activity will implement:{@link #onCreate} is where you initialize your activity.  Most importantly, here you will usually call{@link #setContentView(int)} with a layout resource defining your UI, and using{@link #findViewById} to retrieve the widgets in that UI that you need to interact with programmatically.{@link #onPause} is where you deal with the user leaving your activity.  Most importantly, any changes made by the user should at this  point be committed (usually to the{@link android.content.ContentProvider} holding the data). 上面说Activity的子类一般都会覆写onCreate和onPause两个方法。其中onCreate主要用来初始化Activity所需要的各个UI组件,onPause则可以在用户离开介面的时候保存数据。

下面我们主要看一下Activity的生命周期:

1,Activity的整个生命周期。从onCreate开始初始化,到onDestroy结束释放所有数据。 附上Google注解:The <b>entire lifetime</b> of an activity happens between the first call to{@link android.app.Activity#onCreate} through to a single final call to{@link android.app.Activity#onDestroy}.  An activity will do all setup of "global" state in onCreate(), and release all remaining resources in onDestroy().  For example, if it has a thread running in the background to download data from the network, it may create that thread in onCreate() and then stop the thread in onDestroy().

2,可视化的生命周期。这个生命周期发在onStart调用开始到onStop调用结束之间。而且在Activity的显示和隐藏的切换过程中,这两个方法可以被多次调用。附上Google注解:The <b>visible lifetime</b> of an activity happens between a call to{@link android.app.Activity#onStart} until a corresponding call to{@link android.app.Activity#onStop}.  During this time the user can see the activity on-screen, though it may not be in the foreground and interacting with the user.  Between these two methods you can maintain resources that are needed to show the activity to the user.  For example, you can register a{@link android.content.BroadcastReceiver} in onStart() to monitor for changes that impact your UI, and unregister it in onStop() when the user no longer sees what you are displaying.  The onStart() and onStop() methods can be called multiple times, as the activity becomes visible and hidden to the user.

3,前台生命周期。这个生命周期出现在onResume和onPause方法调用之间。onResume时,Activity处于最前端,也是处于ActivityStack的顶部,用户可以写之交互。由于这两个方法的切换比较频繁,所经代码要尽量是轻量的,能够瞬时完成的,也就是说非耗时的。附上Google注解:The <b>foreground lifetime</b> of an activity happens between a call to{@link android.app.Activity#onResume} until a corresponding call to{@link android.app.Activity#onPause}.  During this time the activity is in front of all other activities and interacting with the user.  An activity can frequently go between the resumed and paused states -- for example when the device goes to sleep, when an activity result is delivered, when a new intent is delivered -- so the code in these methods should be fairly lightweight.

究竟Activity在什么时候保存各种状态和数据,这里也是有说明的,看下面一段:The entire lifecycle of an activity is defined by the following Activity methods.  All of these are hooks that you can override to do appropriate work when the activity changes state.  All activities will implement{@link android.app.Activity#onCreate} to do their initial setup; many will also implement{@link android.app.Activity#onPause} to commit changes to data and otherwise prepare to stop interacting with the user.  You should always call up to your superclass when implementing these methods.

由此可以看出,Activity都是在onCreate中初始化数据,在onPause中保存数据,暂停与用户的交互。上面说的following Activity methods 又是哪些方法呢,下面列出来:

  public class Activity extends ApplicationContext {protected void onCreate(Bundle savedInstanceState);protected void onStart();protected void onRestart();protected void onResume();protected void onPause();protected void onStop();protected void onDestroy();}

以下一段代码是注解中的例子,说的是在onPause方法中保存信息:

 public class CalendarActivity extends Activity {...static final int DAY_VIEW_MODE = 0;static final int WEEK_VIEW_MODE = 1;private SharedPreferences mPrefs;private int mCurViewMode;protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);SharedPreferences mPrefs = getSharedPreferences();mCurViewMode = mPrefs.getInt("view_mode", DAY_VIEW_MODE);}protected void onPause() {super.onPause();SharedPreferences.Editor ed = mPrefs.edit();ed.putInt("view_mode", mCurViewMode);ed.commit();     }}  

关于Activity的其他种种,都可以去参照Google的Activity源码以获得更多的信息。

Acitivity的生命周期相关推荐

  1. Android Acitivity 生命周期

    Fragment 的生命周期: Android Fragment 生命周期及其API使用(建议使用自定义View替换Fragment) Activity的生命周期: (1)启动Activity:系统会 ...

  2. Android四大基本组件介绍与生命周期

    Android四大基本组件分别是Activity,Service服务,Content Provider内容提供者,BroadcastReceiver广播接收器. 一:了解四大基本组件 Activity ...

  3. activity生命周期及数据保存

    为什么80%的码农都做不了架构师?>>>    public class ActivityLife extends Activity {// 在Activity生命周期开始时被调用@ ...

  4. Android四大基本组件和生命周期的介绍

    Android四大基本组件分别是Activity,Service服务,Content Provider内容提供者,BroadcastReceiver广播接收器. 一:了解四大基本组件 Activity ...

  5. Fragment系列总结(一)Fragment概念与生命周期

    写在前面 Fragment是Google在Android3.0新加的东西,它的功能和作用如同名字一样,代表着一块块碎片,而这些碎片则可以灵活地嵌入到各Activity之中. 其他关于Fragment的 ...

  6. Android系列之Fragment(二)----Fragment的生命周期和返回栈

    ​[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/ ...

  7. Android基础_1 四大基本组件介绍与生命周期

    Android四大基本组件分别是Activity,Service(服务),Content Provider(内容提供者),BroadcastReceiver(广播接收器). 一.四大基本组件 Acti ...

  8. Android学习笔记系列四2 —— Activity的生命周期

    2019独角兽企业重金招聘Python工程师标准>>> 启动一个Activity 使用startActivity(Intent intent). intent指定了你想要启动的act ...

  9. Activity生命周期 onCreate onResume onStop onPause

    Android应用开发提高系列(6)--Activity生命周期 onCreate 和 onResume 在程序启动时候都会启动, 所有有些需要在onCreate onResume中都要实现的功能,之 ...

最新文章

  1. Oracle Start With关键字
  2. 论文精读——CenterNet :Objects as Points
  3. 关于若干数据库数据插入性能的对比
  4. Eclipse中tomcat更改部署路径 deply path
  5. (4) ebj学习:ejb发布web service
  6. 禅道启动mysql报错_测试工具之在Linux服务器上部署禅道Bug管理系统
  7. 转载:简单介绍Python中的try和finally和with方法
  8. 十面阿里,菜鸟,天猫,蚂蚁金服题目总汇
  9. bootstap-栅格系统
  10. Apache + Tomcat集群配置详解(1)
  11. linux设备驱动编写基础
  12. 内存问题排查手段及相关文件介绍
  13. 【STM32】【STM32CubeMX】STM32CubeMX的使用之八:低功耗模式及MCU唤醒
  14. 固定ip_1分钟学会查看跨境卫士费用和选择固定IP
  15. leetcode python3 简单题219. Contains Duplicate II
  16. 在MFC中调用DLL .
  17. RxJava Map操作详解
  18. 身份证实名认证java后台代码
  19. 情人节送男生什么礼物好?2022情人节礼物推荐
  20. log4jjavasciprt弹窗拦截

热门文章

  1. 半导体基本知识 PN结的形成及特性
  2. OSPF基本工作原理(上)
  3. 在word 页眉插入章编号+标题
  4. 微信小程序——给用户发送通知
  5. 如何将阿里图标库的引入项目中?
  6. ASP.NET 2.0收集
  7. OKHTTP和retrofit 网络框架集成的有https验证的APP破解抓包
  8. 扫码器:壹码通(EMT 6621)二维码带多个回车换行处理
  9. 112、可燃液体的火灾危险性分类
  10. html怎么设置加qq,qq密友 怎么把QQ好友加为密友啊?