Carson带你学Android:这是一份全面 & 详细的Activity学习指南

前言

  • Activity属于 Android的四大组件之一
  • 本文将全面解析 Activity ,献上1份 Activity的学习攻略,包括其生命周期、启动模式、启动方式等等,希望你们会喜欢。

Carson带你学Android 文章系列:
Carson带你学Android:页面活动-Activity
Carson带你学Android:广播-BroadcastReceiver
Carson带你学Android:服务-Service
Carson带你学Android:内存承载器-ContentProvider


目录

示意图


1. 定义

即 活动,属于 展示型组件

属于Android四大组件之一:ActivityServiceBroadcastReceiverContentProvider


2. 作用

显示界面 & 与用户进行交互

  1. 一个Activity通常是一个界面,是四大组件唯一能被用户感知的
  2. 每个活动被实现为一个独立的类, & 从活动基类继承过来
  3. Activity之间通过Intent进行通信

3. 生命周期

  • 具体如下图

示意图

更加详细请看文章:Android基础:3分钟详解Activity生命周期


4. 启动模式

  • Activity的启动模式有4种,具体如下

示意图

  • 4种启动模式的区别

示意图

更加详细请看文章:Android基础:最易懂的Activity启动模式详解


5. 启动方式

  • 启动Activity的方式主要是:显式Intent & 隐式Intent
  • 具体介绍如下:

5.1 显式Intent(3种)

// 1. 使用构造函数 传入 Class对象
 Intent intent = new Intent(this, SecondActivity.class); 
 startActivity(intent);

// 2. 使用 setClassName()传入 包名+类名 / 包Context+类名
 Intent intent = new Intent(); 
 // 方式1:包名+类名
 // 参数1 = 包名称
 // 参数2 = 要启动的类的全限定名称 
 intent.setClassName("com.hc.hctest", "com.hc.hctest.SecondActivity"); 

 // 方式2:包Context+类名
 // 参数1 = 包Context,可直接传入Activity
 // 参数2 = 要启动的类的全限定名称 
 intent.setClassName(this, "com.hc.hctest.SecondActivity"); 

 startActivity(intent);

// 3. 通过ComponentName()传入 包名 & 类全名
 Intent intent = new Intent(); 
 // 参数1 = 包名称
 // 参数2 = 要启动的类的全限定名称 
 ComponentName cn = new ComponentName("com.hc.hctest", "com.hc.hctest.SecondActivity"); 
 intent.setComponent(cn); 
 startActivity(intent);


5.2 隐式Intent

// 通过Category、Action设置
Intent intent = new Intent(); 
intent.addCategory(Intent.CATEGORY_DEFAULT); 
intent.addCategory("com.hc.second"); 
intent.setAction("com.hc.action"); 
startActivity(intent);

5.3 匹配规则

示意图

更加详细请看文章:Android:关于 Intent组件的那些小事(介绍、使用方法等)


6. 启动过程

Activity的启动过程具体如下:

6.1 示意图

示意图

6.2 具体描述

当请求启动Activity时:

  1. Launcher进程通过Binder驱动向ActivityManagerService类发起startActivity请求;
  2. ActivityManagerService类接收到请求后,向ActivityStack类发送启动Activity的请求;
  3. ActivityStack类记录需启动的Activity的信息 & 调整Activity栈 将其置于栈顶、通过 Binder 驱动 将 Activity 的启动信息传递到ApplicationThread线程中(即Binder线程)
  4. ApplicationThread线程通过HandlerActivity的启动信息发送到主线程ActivityThread
  5. 主线程ActivityThread类接收到该信息 & 请求后,通过ClassLoader机制加载相应的Activity类,最终调用ActivityonCreate(),最后 启动完毕

7. 卡顿原因

Activity的卡顿原因主要归结如下:

示意图

关于内存泄漏 & 性能优化,请看系列文章:
Android性能优化:这是一份全面&详细的内存优化指南
Android性能优化:手把手带你全面了解 内存泄露 & 解决方案
Android性能优化:那些关于Bitmap图片资源优化的小事
Android性能优化:手把手带你全面了解 绘制优化
Android性能优化:布局优化 详细解析(含、、讲解 )


8. 加速启动方式

加速启动Activity的方式归结如下:

示意图


9. 缓存方式(状态保存)

  • 问题描述

示意图

  • 具体说明

示意图


10. Activity 与Fragment的交互方式

至此,关于Android四大组件之一的Activity讲解完毕。


11. 总结

本文全面讲解了 Activity,现在大家对 Activity应该十分了解了

Carson带你学Android 文章系列:
Carson带你学Android:页面活动-Activity
Carson带你学Android:广播-BroadcastReceiver
Carson带你学Android:服务-Service
Carson带你学Android:内存承载器-ContentProvider


欢迎关注Carson_Ho的优快云博客 与 公众号!

博客链接:https://carsonho.blog.youkuaiyun.com/


请帮顶 / 评论点赞!因为你的鼓励是我写作的最大动力!

03-08
### Context in Programming or IT Systems In programming and IT systems, context refers to the environment and conditions under which certain operations are performed. This concept encompasses several aspects depending on the specific area within computing: #### Execution Context Execution context pertains specifically to how code executes at runtime. Each function call creates its own execution context that includes variables, scope chain, and this binding[^1]. For instance, when discussing JavaScript, each time a script starts running or enters a new function, an execution context is created. #### Thread Context Thread context involves all information associated with a particular thread's state including registers, stack pointers, program counter, etc.[^3]. When switching between threads, saving and restoring these contexts ensures continuity of operation without data loss or corruption. #### Security Context Security context defines what actions can be taken by processes based on their permissions level. It plays a critical role in determining access control policies applied to resources like files, network connections, hardware devices, among others. #### Application Context Application context represents settings relevant to applications such as configuration parameters, user preferences, session states, etc., ensuring consistent behavior across different parts of software while maintaining isolation from other programs sharing similar environments. ```python def example_function(): # An execution context is formed here containing local variable definitions, # parameter bindings, parent scopes references (scope chain), along with 'this' value. pass ```
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值