iOS UIViewController
和 Android Activity
生命周期对比
iOS UIViewController |
Android Activity |
描述 |
---|---|---|
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil |
初始化 | |
- (instancetype)init |
初始化 | |
- (void)loadView; |
加载view | |
- (void)viewDidLoad; |
onCreate() |
view加载完成 |
- (void)viewWillAppear:(BOOL)animated; |
onStart() |
view将要显示 |
- (void)viewDidAppear:(BOOL)animated; |
onResume() |
view已经显示了 |
- (void)viewWillDisappear:(BOOL)animated; |
onPause() |
view将要消失 |
- (void)viewDidDisappear:(BOOL)animated; |
onStop() |
view已经消失 |
- (void)dealloc |
onDestroy() |
销毁 |
iOS
和Android
大体上的逻辑是相同的,细微上会有一些差别;
iOS UIViewController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
- (instancetype)init
- (void)loadView;
- (void)viewDidLoad;
- (void)viewWillAppear:(BOOL)animated;
- (void)viewDidAppear:(BOOL)animated;
- (void)viewWillDisappear:(BOOL)animated;
- (void)viewDidDisappear:(BOOL)animated;
- (void)dealloc
打印如下:
-[MyViewController initWithNibName:bundle:]
-[MyViewController init]
-[MyViewController loadView]
-[MyViewController viewDidLoad]
-[MyViewController viewWillAppear:]
-[MyViewController viewDidAppear:]
-[MyViewController viewWillDisappear:]
-[MyViewController viewDidDisappear:]
-[MyViewController dealloc]
present
方式
下面演示了从源控制器 OneViewController
present
到目标控制器 MyViewController
,再dismiss
返回 OneViewController
的过程
OneViewController
加载完成显示顺序:
-[OneViewController initWithNibName:bundle:]
-[OneViewController init]
-[OneViewController loadView]
-[OneViewController viewDidLoad]
-[OneViewController viewWillAppear:]
-[OneViewController viewDidAppear:]
从 OneViewController
通过 present
方式到 MyViewController
过程如下:
-[MyViewController initWithNibName:bundle:]
-[MyViewController init]
-[MyViewController loadView]
-[MyViewController viewDidLoad]
-[OneViewController viewWillDisappear:]
-[MyViewController viewWillAppear:]
-[MyViewController viewDidAppear:]
-[OneViewController viewDidDisappear:]
可以看到需要等到目标控制器完成 viewDidLoad
方法(即加载 view
完成),源控制器才会 Disappear
,目标控制器 Appear
目标控制器 dismiss
过程如下,即从目标控制器返回到源控制器:
-[MyViewController viewWillDisappear:]
-[OneViewController viewWillAppear:]
-[OneViewController viewDidAppear:]
-[MyViewController viewDidDisappear:]
-[MyViewController dealloc]
可以看到,目标控制器 WillDisappear
,等到源控制器 viewDidAppear
了,目标控制器才会真正的 DidDisappear
和销毁 dealloc
小结
- 同一个时刻,只有一个控制器
viewDidAppear
,只有一个控制器viewDidAppear
另外一个控制器才会viewDidDisappear
- 一个控制器
A
的viewWillDisappear
作为一个信号,另外一个控制器B
才会viewWillAppear
和viewDidAppear
,控制器B
viewDidAppear
作为信号,控制器A
才会viewDidDisappear
(可能还有dealloc
)
-[A viewWillDisappear:]
-[B viewWillAppear:]
-[B viewDidAppear:]
-[A viewDidDisappear:]
-[A dealloc]
navigation
方式
下面演示了从源控制器 OneViewController
到目标控制器 MyViewController
,再返回 ViewController
的过程
OneViewController
控制器加载顺序: 和 present
方式相同
-[OneViewController initWithNibName:bundle:]
-[OneViewController init]
-[OneViewController loadView]
-[OneViewController viewDidLoad]
-[OneViewController viewWillAppear:]
-[OneViewController viewDidAppear:]
从 OneViewController
push
到 MyViewController
-[MyViewController initWithNibName:bundle:]
-[MyViewController init]
-[MyViewController loadView]
-[MyViewController viewDidLoad]
-[OneViewController viewWillDisappear:]
-[MyViewController viewWillAppear:]
-[OneViewController viewDidDisappear:]
-[MyViewController viewDidAppear:]
从 MyViewController
pop
到 OneViewController
-[MyViewController viewWillDisappear:]
-[OneViewController viewWillAppear:]
-[MyViewController viewDidDisappear:]
-[OneViewController viewDidAppear:]
-[MyViewController dealloc]
对比:
总结:
-
控制器A
切换到 到控制器B
的大致顺序为:- 1、
[A viewWillDisappear]
- 2、
[B viewWillAppear]
- 3、
[B viewDidAppear]
- 4、
[A viewDidDisappear]
- 1、
- 如果是
present/dismiss
方式,顺序为1234
- 如果是
push/pop
方式,顺序为1243
1、需要一个控制器
viewWillDisappear
作为信号,另外一个控制器开会开始处理;
2、控制器A
到控制器B
,
present/dismiss
方式,需要B viewDidAppear
,然后A viewDidDisappear
push/pop
方式,需要A viewDidDisappear
,然后B viewDidAppear
Android Activity
参考官方图示
[图片上传失败...(image-6cfcaf-1536829354686)]
activity
基本的四种状态
-
resumed
:If an activity is in the foreground of the screen (at the top of the stack), it is active or running.
-
paused
:If an activity has lost focus but is still visible (that is, a new non-full-sized or transparent activity has focus on top of your activity), it is paused. A paused activity is completely alive (it maintains all state and member information and remains attached to the window manager), but can be killed by the system in extreme low memory situations.
-
stopped
:If an activity is completely obscured by another activity, it is stopped. It still retains all state and member information, however, it is no longer visible to the user so its window is hidden and it will often be killed by the system when memory is needed elsewhere.
If an activity is paused or stopped, the system can drop the activity from memory by either asking it to finish, or simply killing its process. When it is displayed again to the user, it must be completely restarted and restored to its previous state.
下面为一开始进入MainActivity
,跳转到 LoginActivity
,又返回到 MainActivity
// 1、MainActivity 创建
E/MainActivity: onCreate --
E/MainActivity: onStart --
E/MainActivity: onResume --
// 2、MainActivity 跳转到 LoginActivity
E/MainActivity: onPause --
E/LoginActivity: onCreate --
E/LoginActivity: onStart --
E/LoginActivity: onResume --
E/MainActivity: onStop --
// 3、LoginActivity 返回到 MainActivity
E/LoginActivity: onPause --
E/MainActivity: onRestart --
E/MainActivity: onStart --
E/MainActivity: onResume --
E/LoginActivity: onStop --
E/LoginActivity: onDestroy --
-
onCreate()
:activity
第一次被创建的时候,一般用作创建views
、绑定数据
、获取Bundle
里面的数据等(页面传递的数据)- 下一步:
onStart()
- 下一步:
-
onRestart()
:activity
被stopped
,然后又需要重新onStart()
,看上面的例子- 下一步:
onStart()
- 下一步:
-
onStart()
:activity
变的对用户可见visible
- 下一步:
-
onResume()
:activity
进入前台foreground
-
onStop()
:activity
被隐藏了hidden
,不可见了 (iOS没有这个逻辑?
)
-
- 下一步:
-
onResume()
:activity
在activity stack
的最上面,可以和用户进行交互了;- 下一步
onPause()
- 下一步
-
onPause()
: 将要跳转到另外一个activity
,包含跳转和返回
- 下一步:
-
onResume()
: 返回 -
onStop()
:activity
被隐藏了hidden
,不可见了 (iOS没有这个逻辑?
)
-
- 下一步:
-
onStop()
:activity
对用户不可见了;1、开启了一个新的activity
覆盖在当前的activity
上面;2、从当前的activity
返回到上一个activity
- 下一步:
-
onRestart()
: 类似2
情况的 上一个activity
-
onDestroy()
: 类似2
情况的 当前的activity
-
- 下一步:
-
onDestroy()
:activity
被销毁了,执行finish()
小结
页面A
跳转到 页面B
-
iOS
:页面B
会走到view加载完成方法
(viewDidLoad
),然后页面A
才会将要消失
(viewWillDisappear
) -
Android
: 然后页面A
才会将要消失
(onPause
),然后才会走页面B
的方法
页面B
跳转到 页面A
类似
Android
类似iOS
的present
方式跳转页面: 都是目标控制器
已经显示
了,源控制器
才会已经消失
了;
Android
比iOS
多了一个onRestart()
方法,即重新启动