An activity is an component that provides a window with which user can interact. The window typically fills the whole screen, or may float on top of other windows.
An application usually consists of multiple activities, one as the main activity and can start other activities based on the user actions.
The system preserves the activities in a stack("back stack") which abides to the "last in, first out" principle. see Tasks and Back Stack. Activity uses callback methods to respond to changes in state in its lifecycle.
Creating an Activity
you must create a subclass of Activity, you need to implement callback methods. The most important methods are:
- onCreate
- invoked when creating your activity. initialize and define the layout for the user interface.
- onPause
- invoked when user is leaving your activity
Implementing a user interface
The user interface for an activity is provided by a hierarchy of views -- objects derived from the View class. Each view controls a particular rectangular space within the activity's window(i.e. Button)
Widgets are views that are visual elements, while Layouts are views derived from ViewGroup that provide a unique layout for its child views.
The most common way to define a layout using views is with an XML layout file.
Delcaring the activity in the manifest
You must declare your activity in the manifest file making it accessible to the system. The android:name is the only required attribute that specifies the class name of the activity. An activity uses intent-filter to declare how other application may activate it.
Starting an Activity
You can activate an activity by creating an intent that explicitly defines the activity you want to start:
Intent intent = new Intent(this, SignInActivity.class);
startActivity(intent);
If you want to activate an external activity to perform some actions for you, you can create the following intent:
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_EMAIL, recipientArray);
startActivity(intent);
sometimes, you want to receive the result from the activity you start, you need to implement the callback method onActivityResult(), then you can get the result in an Intent in your onActivityResult method.
private void pickContact() {
Intent intent = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT_REQUEST);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK && requestCode == PICK_CONTACT_REQUEST) {
Cursor cursor = getContentResolver().query(data.getData()), new String[]{Contacts.DISPLAY_NAME}, null, null, null);
if (cursor.moveToFirst()) {
int columnIndex = cursor.getColumnIndex(Contacts.DISPLAY_NAME);
String name = cursor.getString(columnIndex)
// do something here
}
}
}
Shutting Down an Activity
You can shut down an activity by calling its finish() method, or shut down an activity you previously started by calling finishActivity()
Managing the Activity Lifecycle
You can manage your activity lifecycle by implementing callback methods. An activity can exists in three states:
- Resumed
- in foreground of the screen, also referred to as "running"
- Paused
- another activity is on top of this one, but it is still visible because the top one is partially transparent or don't cover the entire screen.
- A paused activity is still alive but can be killed by the system in extremely low memory situations.
- Stopped
- The activity is completely obscured by another activity.
- A stopped activity is still alive but no longer visible to user and can be killed by the sytem when more memory is needed elsewhere.
分享到:
相关推荐
这个"Itil v3 - Study Notes - Full Package"包含了一个国外专家对ITIL V3全面的学习和复习资料,对于准备ITIL V3认证考试或者想要深入理解IT服务管理的人来说,是非常有价值的资源。 ITIL V3的核心理念是将IT服务...
线性分类问题广义线性模型对数几率回归对数几率函数对数几率函数是一种“Sigmoid函数”,将$z$值转化为$(0,1]$之间的值:模型参数估计。
"android-activity_学习资料"的压缩包很可能包含了关于Activity的深入学习材料,旨在帮助开发者理解并熟练掌握这一关键概念。Activity是Android系统中用于显示用户界面的类,它可以展示一个屏幕,接收用户输入,并对...
这是@hongjilin的学习笔记(小部分是各种博客及官方资料的摘录或整合),旨在方便自己或身边同学的使用与查阅.目前阶段更新偏向前端学习笔记(本人目前是前端岗),但也会进行对Go、Java、Nodejs等后端语言以及对计算机...
Noroff_RJ_Template_Latex 乳胶中的Noroff反射型日记本模板评论: 创建具有7个LaTeX错误的有效.PDF输出,该错误与纸张的几何形状,字体和csquotes软件包有关。 去做: 呈现.PDF文件时,修复当前的LaTeX警告。...
一、当客户端关闭后,服务器不关闭,两次获取Session是否为同一个 二、客户端不关闭,服务器关闭后,两次获取的Session是同一个吗 三、Session什么
【标题】"Study01Android"揭示了这是一个与Android编程学习相关的项目,很可能是初学者入门教程或示例代码集合。在Android开发中,新手通常会通过一系列简单易懂的实例来逐步理解平台的工作原理和编程语法。 【描述...
9. **Android组件通信**:Intent是Android组件间通信的主要方式,理解如何通过Intent启动Activity或Service,以及发送广播。 10. **多媒体支持**:Android提供了丰富的API用于处理音频、视频和图像,如MediaPlayer...
【标题】"study-jam-android-2019-文件" 涉及的是一个关于Android开发的学习资源集合,很可能是Google在2019年举办的一次学习活动"Study Jam"的相关材料。"Study Jam"是Google组织的全球性免费编程学习活动,旨在...
Case study research--design and methods,作者Robert K. Yin
Android-Study-Jams-Resources Android Study Jams:通过预先设计的课程和 Google 认证,学习使用 Kotlin 构建 Android 应用程序 主要网站 Android Study Jam 时间表:2020 年 11 月 25 日 - 2021 年 1 月 25 日
SCJP 310-065 Study Guide-EN SCJP Sun® Certified Programmer for Java™ 6 Study Guide (Exam 310-065) 英文原版
【标题】"GDG-Android-Study-Jam"是一个由Google Developer Groups (GDGs) Twin Cities组织的2015年Android学习活动的代码库,它涵盖了与Android开发相关的实践项目。这个项目旨在帮助初学者和进阶者通过实际编程...
《WebGL编程指南》学习笔记webgl-study-notes
Android应用开发-MP3音乐播放器代码实现,下载测试可正常使用,实现音乐播放功能,有问题留言进行交流
学习笔记 一些学习笔记 壳 deleteCheckPoints 用于定时清理Flink在Hadoop-HDFS产生的状态日志文件中,当前部署在测试172.21.91.206和线上10.18.2.10 sendMessagesToKafka 用于模仿数据流,定时向Kafka插入数据,目前...
Android-Study-Examples Android 的备忘录(教程)来自我的博客 RJ メモ ( ) [Android Studio]入门笔记Part 1 ( ) [Android Studio]入门笔记第二部分 - Activity( )
"STC-STUDY-BOARD4-SCH-C-ASM-VER2.rar"这一压缩包文件包含了STC15系列单片机的实例项目、电路原理图、C语言和汇编语言源码,为学习者提供了全方位的学习材料。 一、实例项目 实例项目是学习单片机编程的最佳途径,...
标题中的"1602display-study-study-hard.zip_UP"指的是一个关于1602液晶显示屏学习项目,其中包含了“study study hard!day day up!”的字样,这可能是一个激励学习者的标语或显示示例。这个项目是基于89C52微控制...
通过这份"SpringBoot2-StudyNotes"的学习资料,你可以深入理解Spring Boot 2的关键概念、配置方式以及如何利用其特性构建高效的应用。同时,通过阅读源代码和实践案例,能够提升你的Java开发能力,使你更加熟练地...