Android 4学习(6):概述 - 深入了解Android Activity
参考:《Professional Android 4 Application Development》
深入了解Android Activity
每一个Android Activity都对应于一个用户界面(UI)。每个Android Application都有一个main Activity,而这个main Activity大多由多个Fragment组成,而这些Fragment后面往往都由一个或多个secondary Activity支持。当用户在不同的界面(窗口)切换时,Android会生成对应的新Activity。
创建Activity
Android提供了Activity类,在编写自己的Activity时,程序员需要继承Activity并重载里面的方法。例如:
package cn.jubincn.activities; import android.app.Activity;
import android.os.Bundle; public class MyActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}
空白的Activity对应的是一个空白屏幕,因此我们需要添加Fragment,Layout和View等UI元素来定制自己的Activity。大部分Activity会占据整个屏幕,除此之外,还有一些半透明的Activity和浮动的Activity。
Android应用程序中,用户交互界面和数据的显示是由View来提供的。Android中的layout类,也叫ViewGroup,可以将View打包进行管理。Fragment则用来组织界面元素,从而更方便地适配不同的界面。给Activity设置界面的方法有两种,一种是创建View类,将其设置到Activity中;另一种是将layout文件设置到Activity中,然后Inflate对应的Activity。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView textView = new TextView(this);
setContentView(textView);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
<activity android:label="@string/app_name" android:name=".MyActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Activity生命周期
Activity的生命周期可以决定Process的优先级,并且Activity需要对生命周期事件进行适当地响应才能提供更好的用户体验。
Activity Stacks
Activity的状态决定于它在Activity Stack中的位置。Activity Stack遵循Stack的“先进后出”的规则:当一个Activity刚被创建时,它会移到栈顶;若用户点击”go back”按钮,或关闭当
前Activity时,栈顶的Activity会被弹出,第二个Activity成为新的栈顶Activity。
Activity State
Activity的生命周期中,有这几种状态:
- Active:在栈顶的Activity,具有最高的优先级,Android会竭力满足它对资源的需求。当其他Activity变为Active状态时,此Activity的状态会变为Paused。
- Paused:处于这个状态的Activity的部分或全部对用户可见,但无法接受用户的输入。
- Stopped:当Activity对用户不可见时,它将转为Stopped状态。此时尽管Activity仍会保留在内存中,但当系统资源紧张时,它会被回收掉。
- Inactive:Activity在启动之前,或被kill之后,处于Inactive状态。
监听并响应Activity State的变化
Activity需要在State发生变化时作出相应的反应,为此Android为Activity的生命周期事件提供了很多相关的ActionHandler,下图展示了Activity的几个生命周期:
对应的代码:
package cn.jubincn.activities;
import android.app.Activity;
import android.os.Bundle;
public class MyStateChangeActivity extends Activity {
// Called at the start of the full lifetime.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Initialize Activity and inflate the UI.
}
// Called after onCreate has finished, use to restore UI state
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// Restore UI state from the savedInstanceState.
// This bundle has also been passed to onCreate. Will only be called if the Activity has been killed by the system since it was last visible.
}
// Called before subsequent visible lifetimes for an Activity process.
@Override
public void onRestart(){
super.onRestart();
// Load changes knowing that the Activity has already been visible within this process.
}
// Called at the start of the visible lifetime.
@Override
public void onStart(){
super.onStart();
// Apply any required UI change now that the Activity is visible.
}
// Called at the start of the active lifetime.
@Override
public void onResume(){
super.onResume();
// Resume any paused UI updates, threads, or processes required
// by the Activity but suspended when it was inactive.
}
// Called to save UI state changes at the end of the active lifecycle.
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
// Save UI state changes to the savedInstanceState.
// This bundle will be passed to onCreate and onRestoreInstanceState if the process is killed and restarted by the run time.
super.onSaveInstanceState(savedInstanceState);
}
// Called at the end of the active lifetime.
@Override
public void onPause(){
// Suspend UI updates, threads, or CPU intensive processes that don’t need to be updated when the Activity isn't the active foreground Activity.
super.onPause();
}
// Called at the end of the visible lifetime.
@Override
public void onStop(){
// Suspend remaining UI updates, threads, or processing that aren’t required when the Activity isn’t visible.
// Persist all edits or state changes as after this call the process is likely to be killed.
super.onStop();
}
// Sometimes called at the end of the full lifetime.
@Override
public void onDestroy(){
// Clean up any resources including ending threads, closing database connections etc.
super.onDestroy();
}
}
Android 4学习(6):概述 - 深入了解Android Activity的更多相关文章
- Android开发学习之路--MAC下Android Studio开发环境搭建
自从毕业开始到现在还没有系统地学习android应用的开发,之前一直都是做些底层的驱动,以及linux上的c开发.虽然写过几个简单的app,也对android4.0.3的源代码做过部分的分析,也算入门 ...
- Android UI学习组件概述
Android的UI组件繁多,如果学习的时候不能自己总结和分类而是学一个记一个不去思考和学习他们内在的联系那真的是只有做Farmer的命了.为了向注定成为Farmer的命运抗争,在学习Android的 ...
- android studio学习----Failed to resolve: com.android.support:design:22.1.1
这个目前好像没有合适的办法,唯一可行的就是 点击那个提示 进行SDK Manager下载就可以了 但是天朝的网啊,我试了很多次,突然的可以下载,运气啊 类似这一系列问题解决办法就是 重新更新SDK ...
- Android 开发学习进程0.12 自定义view activity的属性
设置类似钉钉或tel的圆形用户名首字母头像 设置有两种方法,一是使用已有的库或自定义的view组件,但如果确定只是文字头像,也可使用textview的backgrou属性,调整资源文件使textvie ...
- Android动画学习(二)——Tween Animation
前两天写过一篇Android动画学习的概述,大致的划分了下Android Animation的主要分类,没有看过的同学请移步:Android动画学习(一)——Android动画系统框架简介.今天接着来 ...
- Android 4学习(4):概述 - Using Resources
参考:<Professional Android 4 Application Development> Andorid中的资源包括用户自定义资源和系统自带资源,这两种资源既可以在代码中使用 ...
- Android:日常学习笔记(8)———探究UI开发(5)
Android:日常学习笔记(8)———探究UI开发(5) ListView控件的使用 ListView概述 A view that shows items in a vertically scrol ...
- Android:日常学习笔记(7)———探究UI开发(4)
Android:日常学习笔记(7)———探究UI开发(4) UI概述 View 和 ViewGrou Android 应用中的所有用户界面元素都是使用 View 和 ViewGroup 对象构建而成 ...
- (转载)Android:学习AIDL,这一篇文章就够了(上)
前言 在决定用这个标题之前甚是忐忑,主要是担心自己对AIDL的理解不够深入,到时候大家看了之后说——你这是什么玩意儿,就这么点东西就敢说够了?简直是坐井观天不知所谓——那样就很尴尬了.不过又转念一想, ...
随机推荐
- BZOJ3669 [Noi2014]魔法森林(SPFA+动态加边)
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- dataframe按值(非索引)查找多行
很多情况下,我们会根据一个dataframe里面的值来查找而不是根据索引来查找. 首先我们创建一个dataframe: >>> col = ["id"," ...
- MySQL InnoDB与MyISAM存储引擎差异
言: 之前简单介绍过 MySQL 常用的存储引擎,今天对两个主流的存储简单分析下差异,书上没有参考的笔试题解答注解: 差异: MyISAM 只支持表锁,不支持事务,表损坏率较高.较老的存储引擎. ...
- Https---SSL协议
ssl协议的起源和历史我就不再多说了,就是那个Netscape 网景公司开发的,它的作用主要是提供了一种安全传输方式,我们知道网上有很多的时候需要我们去输入用户名和密码,那么假设我们自己的电脑防病毒还 ...
- 关于WM8741对于32位音频的支持
WM8741据说是支持到32位192K,但实际上,对于32位,它只是支持I2S总线上32位数据输入,内部还是转换成24位来做滤波处理的,DA转换的精度是达不到32位的.不过它在转换到24位的时候有一个 ...
- jquery ajax中使用jsonp的限制(转)
http://www.cnblogs.com/dudu/archive/2012/12/04/jquery_ajax_jsonp.html jsonp 解决的是跨域 ajax 调用的问题.为什么要跨域 ...
- Java演示手机发送短信验证码功能实现
我们这里采用阿里大于的短信API 第一步:登陆阿里大于,下载阿里大于的SDK a.在阿里大于上创建自己的应用 b.点击配置管理中的验证码,先添加签名,再配置短信模板 第二步:解压相关SDK,第一个为j ...
- Python Indentation
In Python, code blocks don't have explicit begin/end or curly braces to mark beginning and end of th ...
- 幸运数字(数位dp)
个人心得:数位dp处理起来是真的麻烦,本来动态规划就够头疼的了,菜的一批. 来看这个题目吧,题目在下面. 把题目变成可以求得就是求前n个数中1-n*9的情况的总和,所以用dp[i][j],表示前i个数 ...
- The Sum of 0 for four numbers(拆解加二分思想)
个人心得:单纯用二分法一直超时,后面发现我的那种方法并没有节省多少时间,后面看了大神的代码,真的是巧妙, 俩个数组分别装a+b,c+d.双指针一个指向最后,从第一个开始想加,加到刚好大于0停止,再看是 ...