Activity基础知识—四大组件

1,414 阅读2分钟

Activity

Activity的生命周期

真的没什么难度,大家自行了解。
有些会问到横竖屏切换的生命周期。

Activity A 启动 Activity B,然后B再返回A,他们的生命周期怎么走

需要考虑一下B是不是透明的,透明盒不透明生命周期是不一样的。
需要考虑 B 的启动模式,不同的启动模式会有一定的区别。

Activity在走了哪个生命周期之后会显示出来

onResume()
简单来说就是:在onResume回调之后,会创建一个 ViewRootImpl ,有了它之后应用端就可以和 WMS 进行双向调用了。

Activity的启动模式有哪些

没什么难度,大家自行了解。

Activity的启动流程

1、点击桌面App图标,Launcher进程采用Binder IPC(AMS)向system_server进程发起startActivity请求; 
2、system_server进程接收到请求后,向zygote进程发送创建进程的请求; 
3、Zygote进程fork出新的子进程,即App进程; 
4、App进程,通过Binder IPC向sytem_server进程发起attachApplication请求; 
5、system_server进程在收到请求后,进行一系列准备工作后,再通过binder IPC向App进程发送scheduleLaunchActivity请求; 
6、App进程的binder线程(ApplicationThread)在收到请求后,通过handler向主线程发送LAUNCH_ACTIVITY消息; 
7、主线程在收到Message后,通过发射机制创建目标Activity,并回调Activity.onCreate()等方法。

Fragment的生命周期,Fragment和Activity之间的传参

需要区别Fragment和Activity之间的生命周期
1.ActivityonCreate();
2.FragmentonAttach();
3.FragmentonCreate();
4.FragmentonCreateView();
5.FragmentonActivityCreated();

接着是这样的:
6.ActivityonStart();
7.FragmentonStart();
8.ActivityonResume();
9.FragmentonResume();

当销毁的时候
10.FragmentonPause();
11.ActivityonPause();
12.FragmentonStop();
13.ActivityonStop();
14.FragmentonDestroyView();
15.FragmentonDestroy();
16.FragmentonDetach();
17.ActivityonDestroy();

Service

Service的生命周期

image.png

IntentService和Service区别

IntentService内部实现了一个线程,可以去执行耗时操作

HandlerThread

继承自Thread,内部创建了一个Looper

Service和Thread的区别还有优缺点

Service 是android的一种机制Service 是运行在主进程的 main 线程上的。
Thread会开辟一个线程去执行它是分配CPU的基本单位。

进程的几种类型

前台进程
可视进程
服务进程
后台进程

Broadcast

有哪几类广播

普通广播(自定义广播)
系统广播
有序广播
粘性广播
应用内广播

LocalBroadcast的实现原理

ContentProvider

image.png