1.Activity介绍

An Activity is an application component that provides a screen with which users can interact in order to do something, such as dial the phone, take a photo, send an email, or view a map. Each activity is given a window in which to draw its user interface. (说白了Activity就是一个和用户交互的界面) The window typically fills the screen, but may be smaller than the screen and float on top of other windows.(一般来说Activity会填满屏幕,也有可能比屏幕小,浮在其他窗口(Activity)的上面)

An application usually consists of multiple activities that are loosely bound to each other. Typically, one activity in an application is specified as the "main" activity,(有的Activity会被设为主Activity(<Activiy>元素里含有<intent-filter>),当程序第一次启动的时候,会显示这个Activity) which is presented to the user when launching the application for the first time. Each activity can then start another activity in order to perform different actions.(一般来说,每个Activity都可以启动其他Activity) Each time a new activity starts, the previous activity is stopped, (每次一个新的Activity启动,前面启动的那个Activity就会停止)but the system preserves the activity in a stack (the "back stack")(但是系统会把这些Activity保存在栈里面). When a new activity starts, it is pushed onto the back stack and takes user focus.(每次一个activit启动,就会被它放到back stack里同,且获得聚集) The back stack abides to the basic "last in, first out" stack mechanism(这个栈是后进先出的), so, when the user is done with the current activity and presses the Back button, it is popped from the stack (and destroyed) and the previous activity resumes.(所以当对当前Activity_2操作完成,要返回Activity_2时,Activity_2会从back stack中弹出,并且被销毁,而Activity_1则会resume) (The back stack is discussed more in the Tasks and Back Stack document.)

When an activity is stopped because a new activity starts, it is notified of this change in state through the activity's lifecycle callback methods. There are several callback methods that an activity might receive,(当一个Activity因为一个新的Activity的启动而改变状态时,会调用相应的回调函数) due to a change in its state—whether the system is creating it, stopping it, resuming it, or destroying it—and each callback provides you the opportunity to perform specific work that's appropriate to that state change. For instance, when stopped, your activity should release any large objects, such as network or database connections(当Activity停止(stop)的时候,应该释放一些大的对象,比如网络连接,数据库连接). When the activity resumes, you can reacquire the necessary resources and resume actions that were interrupted(但是Activity resume的时候,可以继续之前中断的一些操作). These state transitions are all part of the activity lifecycle.

  1.1  back stack 后退栈 (先进后出)([]表示栈)

    应用启动显示Activity_1,栈会插入Activity_1                                [Activity_1];

    跳到Activity_2,栈插入Activity_2                            [Activity_1,Activity_2];

跳转到Activity_3,栈插入Activity_3                                                                          [Activity_1,Activity_2,Activity_3];

    按返回键,Activity_3会从back stack中弹出,并且被销毁,Activity_2会继续(resume)  [Activity_1,Activity_2]

总结:Activity一显示,就会被压到栈中,一按返回按钮,就会被弹出栈,栈先进后出。应用只显示栈顶的Activity

2.创建Activity

写一个类继承Activity,实现一些生命周期的类,比如:created, stopped, resumed, or destroyed
其中下面两个方法是最重要的
onCreate()
You must implement this method.必须实现的方法 The system calls this when creating your activity(系统创建activity的时候就会调用该方法). Within your implementation, you should initialize the essential components of your activity. (在onCreate()实现里,要对activity的组件进行初始化)Most importantly, this is where you must call setContentView() to define the layout for the activity's user interface.(最重要的是还要高用setContentView(),这个方法是用来定义这个activiy的布局的)
onPause()
The system calls this method as the first indication that the user is leaving your activity (though it does not always mean the activity is being destroyed). This is usually where you should commit any changes that should be persisted beyond the current user session (because the user might not come back)(系统会调用这个方法做为第一迹象表明用户要离开这个Activity。这时候,这个方法需要对一些超过当前用户session的“更改”进行保存。虽然不一定意味着这个Activity会被destory,但是用户可能不会再回来).

跟着Android官网学习Activity的更多相关文章

  1. Knockout.Js官网学习(系列)

    1.Knockout.Js官网学习(简介) 2.Knockout.Js官网学习(监控属性Observables) Knockout.Js官网学习(数组observable) 3.Knockout.Js ...

  2. 一劳永逸搭建android开发环境(android官网reference sample api tutorial全下载)

    [摘要]本文简单介绍了android开发环境的搭建,重点介绍了SDK manager和AVD升级问题:并提供了android reference,sample,api,及docs的下载信息. [1]为 ...

  3. 跟着minium官网介绍学习minium-----(一)

    某天,再打开微信开发者工具的时候收到一条推送.说是微信小程序自动化框架 Python 版 -- Minium 公测. Url如下: https://developers.weixin.qq.com/c ...

  4. [Android]官网《monkeyrunner》中文翻译

    以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/5050768.html 翻译自 Android Develope ...

  5. [Android]官网《UI/Application Exerciser Monkey》中文翻译

    以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/5049041.html 翻译自 Android Develope ...

  6. [Android]官网《Testing Support Library》中文翻译

    以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/5048524.html 翻译自 Android Develope ...

  7. [Android开发教程]Android官网developer training中文版教程 - 1.1.1 创建一个Android项目

    本系列持续更新中.转载请注明来源. 前言:近期打算系统学习一下Android开发,发现Android官网上的developer training也是个非常好的学习资料,于是想到一边学习一边写一个中文版 ...

  8. 【Spark深入学习 -16】官网学习SparkSQL

    ----本节内容-------1.概览        1.1 Spark SQL        1.2 DatSets和DataFrame2.动手干活        2.1 契入点:SparkSess ...

  9. android官网被封掉了,只好用这个网站进谷歌了!嘎嘎

         http://developer.android.com/sdk/index.html    这个可以进去,但是必须是搜狐 .360,uc都不用特意FQ     http://173.1 ...

随机推荐

  1. linux netstat命令使用详解

    快速应用 netstat -lnp | more 显示监听的端口 简介 Netstat 命令用于显示各种网络相关信息,如网络连接,路由表,接口状态 (Interface Statistics),mas ...

  2. Amzon MWS API开发之订单接口

    Amazon订单接口是Amazon MWS 开发接口中的一大块,我们可以通过接口调用来获得订单数据. 在调用接口之前,首先我们要获得相关店铺商家的店铺密钥等信息.如下: 在此我将所有信息定义在一个类中 ...

  3. 对Cost (%CPU) 粗略的理解

    今天研究执行计划,看到执行计划里面有Cost (%CPU),我这边研究了一把,不知道对与否,拿出来晒晒 在Oracle 10g中,Oracle 把CPU的cost也统计在执行计划中去了, 这和以前的8 ...

  4. QMenu的个性化定制

    经常使用菜单,菜单的定制相当重要,普通的样式设置不难,一般需求足以实现(QMenu + QAction).如果要足够个性,则需要进行一定的定制.     说起定制,其实也是利用Qt中现成的组件进行组装 ...

  5. Git教程(7)用合并还是变基?

    合并或变基前的样子:分支experiment与master两个分支都产生了提交. 图1. 未合并或变基前的样子 合并 原理: 找到两个分支的最末提交和最近的共同祖先,在执行git merge时所处的分 ...

  6. C#中string.Format()和ToString()格式化方法

    C#数字格式化输出是我们在编程中经常需要处理的事情,那么这里向你介绍了一些C#数字格式化输出的例子,这样就会方便你来选择和比较,什么方式是比较适合自己项目的. int a = 12345678; C# ...

  7. bzoj1056: [HAOI2008]排名系统 && 1862: [Zjoi2006]GameZ游戏排名系统

    hash 加上 平衡树(名次树). 这道题麻烦的地方就在于输入的是一个名字,所以需要hash. 这个hash用的是向后探查避免冲突,如果用类似前向星的方式避免冲突,比较难写,容易挂掉,但也速度快些. ...

  8. 西南科技大学第十一届ACM程序设计大赛发言稿

    西南科技大学第十一届ACM程序设计大赛发言稿 各位老师.志愿者及参赛选手: 大家好,我是来自计科学院卓软1301的哈特13,很荣幸今天能站在这里代表参赛选手发言. 回想起来,我参加ACM比赛已经快两年 ...

  9. 【Grunt】

    GRUNT The JavaScript Task Runnerhttp://gruntjs.com/ Grunt打造前端自动化工作流http://tgideas.qq.com/webplat/inf ...

  10. c语言编程风格

    关于c语言的编程风格,不同的书上有不同的推荐,不同的公司有不同的要求.这里,做了一下基本规范说明. 1.变量定义 在定义变量时,前缀使用变量的类型,之后使用表现变量用途的英文单词或单词缩写,且每个单词 ...