安卓学习笔记之Activity的定义和生命周期篇

       大部分安卓开发的教学资料或者视频,刚开始一般都是介绍安卓系统和开发环境的配置之后,就从Activity开始真正进入技术层面的教授。什么是Activity呢?Activity简单来说就是安卓app的界面,因为直观,适合初学者作为切入点学习。在安卓官方的开发文档中对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 setContentView(View). 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 windowIsFloating set) or embedded inside of another activity (using ActivityGroup). 


          这段话的意思简单来说就是Activity是一个可以和用户交互(就是你可以对它操作它就可以给你相应的反应,你们可以互动)的界面,你可以通过setContentView(View)方法添加一个自己设计的界面。通常Activity是作为全屏的界面使用的,但有时也可以在悬浮窗体和嵌套的Activity中使用(这两者现在暂且不谈)。

          Activity的界面通常是由各种控件(就是各种按钮、进度条什么的)组成的,所以Activity可以看成一个控件的容器。


       关于Activity的生命周期在安卓官方文档的解释如下:

      

     Activities in the system are managed as an activity stack. When a new activity is started, it is placed on the top of the stack and becomes the running activity -- the previous activity always remains below it in the stack, and will not come to the foreground again until the new activity exits.

      An activity has essentially four states:

  • If an activity in the foreground of the screen (at the top of the stack), it is active or running.
  • If an activity has lost focus but is still visible (that is, a new non-full-sized or transparent activity has focus on top of your activity), it is paused. A paused activity is completely alive (it maintains all state and member information and remains attached to the window manager), but can be killed by the system in extreme low memory situations.
  • If an activity is completely obscured by another activity, it is stopped. It still retains all state and member information, however, it is no longer visible to the user so its window is hidden and it will often be killed by the system when memory is needed elsewhere.
  • If an activity is paused or stopped, the system can drop the activity from memory by either asking it to finish, or simply killing its process. When it is displayed again to the user, it must be completely restarted and restored to its previous state.

      第一段大致意思就是安卓系统对Activity的管理是生成一个栈将各个创建出来的Activity放到栈中。每次创建新的Activity就将其实例压入该栈,销毁的时候从上到下依次出栈。(例如你在一个软件中不断按后退键时一个一个之前用过的界面以和进入相反的顺序出现) 。一般不在栈顶的Activity会继续保持,也不会再栈顶的Activity离开前成为栈顶Activity。一个Activity有四种状态,结合官方文档的一个流程图来说会简单得多:

         

       由上图可知,创建一个Activity1方法,系统会自动调用onCreate方法(一般各种控件的初始化在这个函数中完成),接着调用onStart方法,这个方法让Activity1界面可见,接着调用onResume方法,让Activity1获得焦点,即可编辑(你可以和它互动了),此时Activity1进入running的状态,即出现在前台而且在Activity栈的顶部。

       此时如果要跳转到另外一个Activity2中,就会执行onPause方法,失去了焦点,即不可以编辑了。此时Activity1仍然存活且保存着之前的信息,但是在Activity栈顶部的是Activity2。而且当系统内存极度不足时Activity1可能被回收。

     假如Activity2完全覆盖Activity1,则Activity1会执行onStop方法,让其不可见,此时Activity1仍然存活且保存着之前的信息,但是当系统有需要时可以将其回收此时Activity2如果被销毁了(按了后退键),Activity1执行onRestart方法和onStart方法,重新可见并获得焦点。

      假如Activity2没有完全覆盖Activity1,则Activity1不会执行onStop方法。此时Activity2如果被销毁了(按了后退键),Activity1执行onResume重新获得焦点。此时如果按后退键Activity1执行onPause--onStop--onDestroy方法,将Activity1从栈弹出,此时Activity被销毁。

       

     总结来说:

                 完整生命周期:onCreate--onStart--onResume--onPause--onStop--onDestroy

                 可见生命周期:onstart--onResume--onPause--onStop

                 前台生命周期:onResume--onPause(原Activity可见)

                 第一次开启Activity:onCreate--onStart--onResume

                 退出Activity:onPause--onStop--onDestroy

                 点击小房子执行onPause--onStop。(最小化)

                 点击后退键执行onPause--onStop--onDestroy.(为了保护数据,一般把要保护的数据在这三个方法中保存)

       

    第一次写技术博客,写的东西很基础,表达也不都生动清晰,请各位包容。下篇准备写Activity的跳转和Activity间的通信。




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值